cryptopasta: Index | Examples | Files

package cryptopasta

import ""

Provides symmetric authenticated encryption using 256-bit AES-GCM with a random nonce.

Provides a recommended hashing algorithm.

The hash function is HMAC-SHA512/256 where SHA512/256 is as described in FIPS 180-4. This construction avoids length-extension attacks while maintaining a widely compatible digest size with better performance on 64-bit systems.

Password hashing uses bcrypt with a work factor of 14.

Provides encoding and decoding routines for various cryptographic structures.

Provides message authentication and asymmetric signatures.

Message authentication: HMAC SHA512/256 This is a slight twist on the highly dependable HMAC-SHA256 that gains performance on 64-bit systems and consistency with our hashing recommendation.

Asymmetric Signature: ECDSA using P256 and SHA256 ECDSA is the best compromise between cryptographic concerns and support for our internal use cases (e.g. RFC7518). The Go standard library implementation has some protection against entropy problems, but is not deterministic. See

Provides a recommended TLS configuration.



Package Files

encrypt.go hash.go marshal.go sign.go tls.go

func CheckHMAC Uses

func CheckHMAC(data, suppliedMAC []byte, key *[32]byte) bool

CheckHMAC securely checks the supplied MAC against a message using the shared secret key.

func CheckPasswordHash Uses

func CheckPasswordHash(hash, password []byte) error

CheckPassword securely compares a bcrypt hashed password with its possible plaintext equivalent. Returns nil on success, or an error on failure.

func DecodePrivateKey Uses

func DecodePrivateKey(encodedKey []byte) (*ecdsa.PrivateKey, error)

DecodePrivateKey decodes a PEM-encoded ECDSA private key.

func DecodePublicKey Uses

func DecodePublicKey(encodedKey []byte) (*ecdsa.PublicKey, error)

DecodePublicKey decodes a PEM-encoded ECDSA public key.

func DecodeSignatureJWT Uses

func DecodeSignatureJWT(b64sig string) ([]byte, error)

Decodes an ECDSA signature according to

func Decrypt Uses

func Decrypt(ciphertext []byte, key *[32]byte) (plaintext []byte, err error)

Decrypt decrypts data using 256-bit AES-GCM. This both hides the content of the data and provides a check that it hasn't been altered. Expects input form nonce|ciphertext|tag where '|' indicates concatenation.

func DefaultTLSConfig Uses

func DefaultTLSConfig() *tls.Config

func EncodePrivateKey Uses

func EncodePrivateKey(key *ecdsa.PrivateKey) ([]byte, error)

EncodePrivateKey encodes an ECDSA private key to PEM format.

func EncodePublicKey Uses

func EncodePublicKey(key *ecdsa.PublicKey) ([]byte, error)

EncodePublicKey encodes an ECDSA public key to PEM format.

func EncodeSignatureJWT Uses

func EncodeSignatureJWT(sig []byte) string

Encodes an ECDSA signature according to

func Encrypt Uses

func Encrypt(plaintext []byte, key *[32]byte) (ciphertext []byte, err error)

Encrypt encrypts data using 256-bit AES-GCM. This both hides the content of the data and provides a check that it hasn't been altered. Output takes the form nonce|ciphertext|tag where '|' indicates concatenation.

func GenerateHMAC Uses

func GenerateHMAC(data []byte, key *[32]byte) []byte

GenerateHMAC produces a symmetric signature using a shared secret key.

func Hash Uses

func Hash(tag string, data []byte) []byte

Hash generates a hash of data using HMAC-SHA-512/256. The tag is intended to be a natural-language string describing the purpose of the hash, such as "hash file for lookup key" or "master secret to client secret". It serves as an HMAC "key" and ensures that different purposes will have different hash output. This function is NOT suitable for hashing passwords.


tag := "hashing file for lookup key"
contents, err := ioutil.ReadFile("testdata/random")
if err != nil {
    fmt.Printf("could not read file: %v\n", err)
digest := Hash(tag, contents)



func HashPassword Uses

func HashPassword(password []byte) ([]byte, error)

HashPassword generates a bcrypt hash of the password using work factor 14.

func NewEncryptionKey Uses

func NewEncryptionKey() *[32]byte

NewEncryptionKey generates a random 256-bit key for Encrypt() and Decrypt(). It panics if the source of randomness fails.

func NewHMACKey Uses

func NewHMACKey() *[32]byte

NewHMACKey generates a random 256-bit secret key for HMAC use. Because key generation is critical, it panics if the source of randomness fails.

func NewSigningKey Uses

func NewSigningKey() (*ecdsa.PrivateKey, error)

NewSigningKey generates a random P-256 ECDSA private key.

func Sign Uses

func Sign(data []byte, privkey *ecdsa.PrivateKey) ([]byte, error)

Sign signs arbitrary data using ECDSA.

func Verify Uses

func Verify(data, signature []byte, pubkey *ecdsa.PublicKey) bool

Verify checks a raw ECDSA signature. Returns true if it's valid and false if not.

Package cryptopasta imports 17 packages (graph) and is imported by 53 packages. Updated 2017-06-10. Refresh now. Tools for package owners.