4 crypto_rand "crypto/rand" // Custom so it's clear which rand we're using.
8 "golang.org/x/crypto/nacl/box"
12 senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
17 recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
22 // You must use a different nonce for each message you encrypt with the
23 // same key. Since the nonce here is 192 bits long, a random value
24 // provides a sufficiently small probability of repeats.
26 if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
30 msg := []byte("Alas, poor Yorick! I knew him, Horatio")
31 // This encrypts msg and appends the result to the nonce.
32 encrypted := box.Seal(nonce[:], msg, &nonce, recipientPublicKey, senderPrivateKey)
34 // The recipient can decrypt the message using their private key and the
35 // sender's public key. When you decrypt, you must use the same nonce you
36 // used to encrypt the message. One way to achieve this is to store the
37 // nonce alongside the encrypted message. Above, we stored the nonce in the
38 // first 24 bytes of the encrypted text.
39 var decryptNonce [24]byte
40 copy(decryptNonce[:], encrypted[:24])
41 decrypted, ok := box.Open(nil, encrypted[24:], &decryptNonce, senderPublicKey, recipientPrivateKey)
43 panic("decryption error")
45 fmt.Println(string(decrypted))
46 // Output: Alas, poor Yorick! I knew him, Horatio
49 func Example_precompute() {
50 senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
55 recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
60 // The shared key can be used to speed up processing when using the same
61 // pair of keys repeatedly.
62 sharedEncryptKey := new([32]byte)
63 box.Precompute(sharedEncryptKey, recipientPublicKey, senderPrivateKey)
65 // You must use a different nonce for each message you encrypt with the
66 // same key. Since the nonce here is 192 bits long, a random value
67 // provides a sufficiently small probability of repeats.
69 if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
73 msg := []byte("A fellow of infinite jest, of most excellent fancy")
74 // This encrypts msg and appends the result to the nonce.
75 encrypted := box.SealAfterPrecomputation(nonce[:], msg, &nonce, sharedEncryptKey)
77 // The shared key can be used to speed up processing when using the same
78 // pair of keys repeatedly.
79 var sharedDecryptKey [32]byte
80 box.Precompute(&sharedDecryptKey, senderPublicKey, recipientPrivateKey)
82 // The recipient can decrypt the message using the shared key. When you
83 // decrypt, you must use the same nonce you used to encrypt the message.
84 // One way to achieve this is to store the nonce alongside the encrypted
85 // message. Above, we stored the nonce in the first 24 bytes of the
87 var decryptNonce [24]byte
88 copy(decryptNonce[:], encrypted[:24])
89 decrypted, ok := box.OpenAfterPrecomputation(nil, encrypted[24:], &decryptNonce, &sharedDecryptKey)
91 panic("decryption error")
93 fmt.Println(string(decrypted))
94 // Output: A fellow of infinite jest, of most excellent fancy