crypto

package
v0.0.0-...-aeb4a1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package crypto handles all encryption, decryption, hashing, and parsing of keys.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeStringB64

func DecodeStringB64(stringB64 string) ([]byte, error)

DecodeStringB64 parses a B64 string into a []byte.

Example
b64String := base64.StdEncoding.EncodeToString([]byte("data"))

DecodeStringB64(b64String)
Output:

func DecryptWithPrivateKey

func DecryptWithPrivateKey(privateKey *rsa.PrivateKey, data []byte) ([]byte, error)

DecryptWithPrivateKey decrypts data using the provided RSA private key.

Example
data := []byte("data")
privateKey := GeneratePrivateKey()
publicKey := privateKey.Public().(*rsa.PublicKey)

encryptedData, _ := EncryptWithPublicKey(publicKey, data)

DecryptWithPrivateKey(privateKey, encryptedData)
Output:

func DecryptWithSymKey

func DecryptWithSymKey(symKey []byte, encryptedData []byte) ([]byte, error)

DecryptWithSymKey decrypts data with the provided AES sym key.

Example
data := []byte("data")
symKey := GenerateSymKey()

encryptedData, _ := EncryptWithSymKey(symKey, data)

DecryptWithSymKey(symKey, encryptedData)
Output:

func EncodeToB64String

func EncodeToB64String(data []byte) string

EncodeToB64String encodes data to a b64 string.

Example
data := []byte("data")

EncodeToB64String(data)
Output:

func EncryptWithPublicKey

func EncryptWithPublicKey(publicKey *rsa.PublicKey, data []byte) ([]byte, error)

EncryptWithPublicKey encrypts data using the provided RSA public key.

Example
data := []byte("data")
privateKey := GeneratePrivateKey()
publicKey := privateKey.Public().(*rsa.PublicKey)

EncryptWithPublicKey(publicKey, data)
Output:

func EncryptWithSymKey

func EncryptWithSymKey(symKey []byte, data []byte) ([]byte, error)

EncryptWithSymKey encrypts data using the provided AES sym key.

Example
data := []byte("data")
symKey := GenerateSymKey()

EncryptWithSymKey(symKey, data)
Output:

func GeneratePrivateKey

func GeneratePrivateKey() *rsa.PrivateKey

GeneratePrivateKey generates a random 2048-bit RSA Private Key.

func GenerateSymKey

func GenerateSymKey() []byte

GenerateSymKey generates a random 32-byte AES symmetric key.

func GetSymKeyFromHash

func GetSymKeyFromHash(seed []byte) []byte

GetSymKeyFromHash returns a sym key derived from the hash of the input seed.

Example
data := []byte("data")

GetSymKeyFromHash(data)
Output:

func Hash

func Hash(key []byte) []byte

Hash returns the hash of the given key using SHA-256.

Example
data := []byte("data")

Hash(data)
Output:

func HashB64

func HashB64(key []byte) string

HashB64 returns the base64 encoding of the SHA-256 hash of the given key.

func HashLong

func HashLong(key []byte) []byte

HashLong returns the SHA-512 of the given key. len=32

func HashLongB64

func HashLongB64(key []byte) string

HashLongB64 returns the base64 encoding of the SHA-512 hash of the given key.

func HashShort

func HashShort(key []byte) []byte

HashShort returns the hash of the given key using SHA-1.

func HashShortB64

func HashShortB64(key []byte) string

HashShortB64 returns the base64 encoding of the SHA-1 hash of the given key.

func MarshalPrivateKey

func MarshalPrivateKey(key *rsa.PrivateKey) []byte

MarshalPrivateKey marshals a *rsa.PrivateKey into a []byte.

Example
privateKey := GeneratePrivateKey()

MarshalPrivateKey(privateKey)
Output:

func ParsePrivateKey

func ParsePrivateKey(key []byte) (*rsa.PrivateKey, error)

ParsePrivateKey parses key bytes into a *rsa.PrivateKey.

Example
privateKeyBytes := MarshalPrivateKey(GeneratePrivateKey())

ParsePrivateKey(privateKeyBytes)
Output:

func ParsePrivateKeyB64

func ParsePrivateKeyB64(privateKeyB64 string) (*rsa.PrivateKey, error)

ParsePrivateKeyB64 parses a B64 key string into a *rsa.PrivateKey.

Example
privateKey := GeneratePrivateKey()
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyB64 := base64.StdEncoding.EncodeToString(privateKeyBytes)

ParsePrivateKeyB64(privateKeyB64)
Output:

func ParsePublicKey

func ParsePublicKey(key []byte) (*rsa.PublicKey, error)

ParsePublicKey parses key bytes into a *rsa.PublicKey.

Example
privateKey := GeneratePrivateKey()
publicKeyBytes, _ := x509.MarshalPKIXPublicKey(privateKey.Public())

ParsePublicKey(publicKeyBytes)
Output:

func ParsePublicKeyB64

func ParsePublicKeyB64(publicKeyB64 string) (*rsa.PublicKey, error)

ParsePublicKeyB64 parses a B64 key string into a *rsa.PublicKey.

Example
privateKey := GeneratePrivateKey()
publicKeyBytes, _ := x509.MarshalPKIXPublicKey(privateKey.Public())
publicKeyB64 := base64.StdEncoding.EncodeToString(publicKeyBytes)

ParsePublicKeyB64(publicKeyB64)
Output:

func ParseSymKeyB64

func ParseSymKeyB64(symKeyB64 string) ([]byte, error)

ParseSymKeyB64 decodes a B64 sym key, confirms the result is a valid sym key, and returns. If not, return error.

Example
symKeyB64 := base64.StdEncoding.EncodeToString(GenerateSymKey())

ParseSymKeyB64(symKeyB64)
Output:

func PrivateKeyToBytes

func PrivateKeyToBytes(privateKey *rsa.PrivateKey) []byte

PrivateKeyToBytes marshals a *rsa.PrivateKey into a []byte.

Example
privateKey := GeneratePrivateKey()

PrivateKeyToBytes(privateKey)
Output:

func PublicKeyToBytes

func PublicKeyToBytes(publicKey *rsa.PublicKey) []byte

PublicKeyToBytes marshals a *rsa.PublicKey into a []byte.

Example
privateKey := GeneratePrivateKey()
publicKey := privateKey.Public().(*rsa.PublicKey)

PublicKeyToBytes(publicKey)
Output:

func ValidatePrivateKey

func ValidatePrivateKey(key []byte) bool

ValidatePrivateKey returns true if the key is a valid RSA private key.

Example
privateKey := GeneratePrivateKey()
privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)

validPrivateKey := ValidatePrivateKey(privateKeyBytes)

fmt.Println(validPrivateKey)
Output:

true

func ValidatePublicKey

func ValidatePublicKey(key []byte) bool

ValidatePublicKey returns true if the key is a valid RSA public key.

Example
privateKey := GeneratePrivateKey()
publicKeyBytes, _ := x509.MarshalPKIXPublicKey(privateKey.Public())

validPublicKey := ValidatePublicKey(publicKeyBytes)

fmt.Println(validPublicKey)
Output:

true

func ValidateSymKey

func ValidateSymKey(key []byte) bool

ValidateSymKey validates a key is a sym key by checking its length.

Example
symKey := GenerateSymKey()

validSymKey := ValidateSymKey(symKey)

fmt.Println(validSymKey)
Output:

true

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL