HowtoGo
Home / Standard Library / The crypto Package
Standard Library

The crypto Package

crypto defines the shared Hash type and key interfaces every algorithm-specific subpackage implements. This page covers that core plus the subpackages reached for daily: crypto/rand, the common hash functions, and crypto/hmac. Asymmetric algorithms like crypto/rsa and crypto/tls get their own pages.

sha256.Sum256 and its siblings take a []byte and return a fixed-size array, the shortest path from data to digest.

sum := sha256.Sum256([]byte("hello"))
fmt.Printf("%x\n", sum)

Examples

Sum functions take the whole input at once and return a fixed-size array. The array's length differs by algorithm; the call pattern is identical across the package.

sum256 := sha256.Sum256([]byte("gopher"))
sum1 := sha1.Sum([]byte("gopher"))
sumMD5 := md5.Sum([]byte("gopher"))

fmt.Printf("%x\n", sum256)
fmt.Printf("%x\n", sum1)
fmt.Printf("%x\n", sumMD5)
Output
9cc1ee455a3363ffc504f40006f70d0c8276648a5d3eb3f9524e94d1b7a83aef
4188736a00fbfb506aca06281acf338290455c21
ca654591d4ac97414391907f882b3c05

crypto package core

FunctionDescription
Hash uint
crypto.SHA256
Identifies a hash algorithm, such as crypto.SHA256, without importing its implementation directly
MD5, SHA1, SHA224, SHA256, SHA384, SHA512
crypto.SHA256
The most common predefined Hash identifiers
(h Hash) New() hash.Hash
crypto.SHA256.New()
Returns a new hash.Hash for h, panicking if the implementation isn't registered
(h Hash) Available() bool
crypto.MD5.Available()
Reports whether h's implementation is linked into the binary
(h Hash) Size() int
crypto.SHA256.Size()
The digest size h produces, in bytes
RegisterHash(h Hash, f func() hash.Hash)
(called internally by crypto/sha256)
Registers the constructor an algorithm's package calls from its own init function
PublicKey, PrivateKey any
var pub crypto.PublicKey
Opaque marker interfaces satisfied by every key type across the crypto subpackages
Signer, SignerOpts
signer.Sign(rand.Reader, digest, opts)
The interface an asymmetric key type implements to produce a signature
Decrypter, DecrypterOpts
decrypter.Decrypt(rand.Reader, msg, opts)
The interface an asymmetric key type implements to decrypt ciphertext

hash.Hash interface

FunctionDescription
Write(p []byte) (n int, err error)
h.Write(chunk)
Adds more data to the running hash; the error is always nil
Sum(b []byte) []byte
h.Sum(nil)
Appends the current digest to b and returns the result; pass nil for just the digest
Reset()
h.Reset()
Restores the hash to its initial, empty state
Size() int
h.Size()
The digest size in bytes
BlockSize() int
h.BlockSize()
The algorithm's internal block size, for callers that need to align writes

crypto/rand

FunctionDescription
Reader io.Reader
rand.Reader
A shared, cryptographically secure random source backed by the operating system
Read(b []byte) (n int, err error)
rand.Read(b)
Fills b with cryptographically secure random bytes
Int(rand io.Reader, max *big.Int) (*big.Int, error)
rand.Int(rand.Reader, big.NewInt(100))
A uniform random integer in the range [0, max)
Prime(rand io.Reader, bits int) (*big.Int, error)
rand.Prime(rand.Reader, 256)
A random prime number with the given bit length

Common hash implementations

FunctionDescription
sha256.Sum256(data []byte) [32]byte / sha256.New() hash.Hash
sha256.Sum256(data)
SHA-256 in one call, or as a streaming hash.Hash
sha256.Sum224(data []byte) [28]byte / sha256.New224() hash.Hash
sha256.Sum224(data)
The truncated SHA-224 variant
sha512.Sum512(data []byte) [64]byte / sha512.New() hash.Hash
sha512.Sum512(data)
SHA-512, and its truncated SHA-384 variant via Sum384 / New384
sha1.Sum(data []byte) [20]byte / sha1.New() hash.Hash
sha1.Sum(data)
SHA-1, kept for compatibility; avoid it for new security-sensitive uses
md5.Sum(data []byte) [16]byte / md5.New() hash.Hash
md5.Sum(data)
MD5, kept for compatibility; not safe against a determined attacker

crypto/hmac

FunctionDescription
New(h func() hash.Hash, key []byte) hash.Hash
hmac.New(sha256.New, key)
Wraps a hash constructor into an HMAC keyed with key
Equal(mac1, mac2 []byte) bool
hmac.Equal(sum, expected)
Constant-time comparison, safe for verifying a MAC without leaking timing information