Documentation ¶
Overview ¶
Package crypto handles all encryption, decryption, hashing, and parsing of keys.
Index ¶
- func DecodeStringB64(stringB64 string) ([]byte, error)
- func DecryptWithPrivateKey(privateKey *rsa.PrivateKey, data []byte) ([]byte, error)
- func DecryptWithSymKey(symKey []byte, encryptedData []byte) ([]byte, error)
- func EncodeToB64String(data []byte) string
- func EncryptWithPublicKey(publicKey *rsa.PublicKey, data []byte) ([]byte, error)
- func EncryptWithSymKey(symKey []byte, data []byte) ([]byte, error)
- func GeneratePrivateKey() *rsa.PrivateKey
- func GenerateSymKey() []byte
- func GetSymKeyFromHash(seed []byte) []byte
- func Hash(key []byte) []byte
- func HashB64(key []byte) string
- func HashLong(key []byte) []byte
- func HashLongB64(key []byte) string
- func HashShort(key []byte) []byte
- func HashShortB64(key []byte) string
- func MarshalPrivateKey(key *rsa.PrivateKey) []byte
- func ParsePrivateKey(key []byte) (*rsa.PrivateKey, error)
- func ParsePrivateKeyB64(privateKeyB64 string) (*rsa.PrivateKey, error)
- func ParsePublicKey(key []byte) (*rsa.PublicKey, error)
- func ParsePublicKeyB64(publicKeyB64 string) (*rsa.PublicKey, error)
- func ParseSymKeyB64(symKeyB64 string) ([]byte, error)
- func PrivateKeyToBytes(privateKey *rsa.PrivateKey) []byte
- func PublicKeyToBytes(publicKey *rsa.PublicKey) []byte
- func ValidatePrivateKey(key []byte) bool
- func ValidatePublicKey(key []byte) bool
- func ValidateSymKey(key []byte) bool
Examples ¶
- DecodeStringB64
- DecryptWithPrivateKey
- DecryptWithSymKey
- EncodeToB64String
- EncryptWithPublicKey
- EncryptWithSymKey
- GetSymKeyFromHash
- Hash
- MarshalPrivateKey
- ParsePrivateKey
- ParsePrivateKeyB64
- ParsePublicKey
- ParsePublicKeyB64
- ParseSymKeyB64
- PrivateKeyToBytes
- PublicKeyToBytes
- ValidatePrivateKey
- ValidatePublicKey
- ValidateSymKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeStringB64 ¶
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 ¶
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 ¶
EncodeToB64String encodes data to a b64 string.
Example ¶
data := []byte("data") EncodeToB64String(data)
Output:
func EncryptWithPublicKey ¶
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 ¶
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 ¶
GetSymKeyFromHash returns a sym key derived from the hash of the input seed.
Example ¶
data := []byte("data") GetSymKeyFromHash(data)
Output:
func Hash ¶
Hash returns the hash of the given key using SHA-256.
Example ¶
data := []byte("data") Hash(data)
Output:
func HashLongB64 ¶
HashLongB64 returns the base64 encoding of the SHA-512 hash of the given key.
func HashShortB64 ¶
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 ¶
ParsePublicKey parses key bytes into a *rsa.PublicKey.
Example ¶
privateKey := GeneratePrivateKey() publicKeyBytes, _ := x509.MarshalPKIXPublicKey(privateKey.Public()) ParsePublicKey(publicKeyBytes)
Output:
func ParsePublicKeyB64 ¶
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 ¶
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 ¶
PublicKeyToBytes marshals a *rsa.PublicKey into a []byte.
Example ¶
privateKey := GeneratePrivateKey() publicKey := privateKey.Public().(*rsa.PublicKey) PublicKeyToBytes(publicKey)
Output:
func ValidatePrivateKey ¶
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 ¶
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 ¶
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.