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)9cc1ee455a3363ffc504f40006f70d0c8276648a5d3eb3f9524e94d1b7a83aef
4188736a00fbfb506aca06281acf338290455c21
ca654591d4ac97414391907f882b3c05hash.Hash lets data arrive in pieces, useful for hashing a file or network stream without holding it all in memory. Writing the same bytes in two calls or one produces an identical digest.
h := sha256.New()
h.Write([]byte("go"))
h.Write([]byte("pher"))
fmt.Printf("%x\n", h.Sum(nil))9cc1ee455a3363ffc504f40006f70d0c8276648a5d3eb3f9524e94d1b7a83aefcrypto/rand reads from the operating system's cryptographically secure source, unlike math/rand's predictable, seed-based sequence. Int returns a value uniformly distributed across the requested range.
b := make([]byte, 16)
_, err := rand.Read(b)
fmt.Println(len(b), err)
n, err := rand.Int(rand.Reader, big.NewInt(100))
fmt.Println(n.Cmp(big.NewInt(0)) >= 0, n.Cmp(big.NewInt(100)) < 0, err)16
true true hmac.Equal runs in constant time regardless of where the two byte slices first differ, closing a timing side channel that a plain == or bytes.Equal comparison could leak.
key := []byte("secret-key")
msg := []byte("transfer:100")
mac := hmac.New(sha256.New, key)
mac.Write(msg)
sum := mac.Sum(nil)
fmt.Printf("%x\n", sum)
expected, _ := hex.DecodeString("e9db1b37c303c5a835c319e62f7c1212348ab0f1f3cd64db09b0d05d39dea135")
fmt.Println(hmac.Equal(sum, expected))e9db1b37c303c5a835c319e62f7c1212348ab0f1f3cd64db09b0d05d39dea135
truecrypto package core
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
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 |