You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
3.3 KiB

  1. package box_test
  2. import (
  3. crypto_rand "crypto/rand" // Custom so it's clear which rand we're using.
  4. "fmt"
  5. "io"
  6. "golang.org/x/crypto/nacl/box"
  7. )
  8. func Example() {
  9. senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
  10. if err != nil {
  11. panic(err)
  12. }
  13. recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
  14. if err != nil {
  15. panic(err)
  16. }
  17. // You must use a different nonce for each message you encrypt with the
  18. // same key. Since the nonce here is 192 bits long, a random value
  19. // provides a sufficiently small probability of repeats.
  20. var nonce [24]byte
  21. if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
  22. panic(err)
  23. }
  24. msg := []byte("Alas, poor Yorick! I knew him, Horatio")
  25. // This encrypts msg and appends the result to the nonce.
  26. encrypted := box.Seal(nonce[:], msg, &nonce, recipientPublicKey, senderPrivateKey)
  27. // The recipient can decrypt the message using their private key and the
  28. // sender's public key. When you decrypt, you must use the same nonce you
  29. // used to encrypt the message. One way to achieve this is to store the
  30. // nonce alongside the encrypted message. Above, we stored the nonce in the
  31. // first 24 bytes of the encrypted text.
  32. var decryptNonce [24]byte
  33. copy(decryptNonce[:], encrypted[:24])
  34. decrypted, ok := box.Open(nil, encrypted[24:], &decryptNonce, senderPublicKey, recipientPrivateKey)
  35. if !ok {
  36. panic("decryption error")
  37. }
  38. fmt.Println(string(decrypted))
  39. // Output: Alas, poor Yorick! I knew him, Horatio
  40. }
  41. func Example_precompute() {
  42. senderPublicKey, senderPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
  43. if err != nil {
  44. panic(err)
  45. }
  46. recipientPublicKey, recipientPrivateKey, err := box.GenerateKey(crypto_rand.Reader)
  47. if err != nil {
  48. panic(err)
  49. }
  50. // The shared key can be used to speed up processing when using the same
  51. // pair of keys repeatedly.
  52. sharedEncryptKey := new([32]byte)
  53. box.Precompute(sharedEncryptKey, recipientPublicKey, senderPrivateKey)
  54. // You must use a different nonce for each message you encrypt with the
  55. // same key. Since the nonce here is 192 bits long, a random value
  56. // provides a sufficiently small probability of repeats.
  57. var nonce [24]byte
  58. if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
  59. panic(err)
  60. }
  61. msg := []byte("A fellow of infinite jest, of most excellent fancy")
  62. // This encrypts msg and appends the result to the nonce.
  63. encrypted := box.SealAfterPrecomputation(nonce[:], msg, &nonce, sharedEncryptKey)
  64. // The shared key can be used to speed up processing when using the same
  65. // pair of keys repeatedly.
  66. var sharedDecryptKey [32]byte
  67. box.Precompute(&sharedDecryptKey, senderPublicKey, recipientPrivateKey)
  68. // The recipient can decrypt the message using the shared key. When you
  69. // decrypt, you must use the same nonce you used to encrypt the message.
  70. // One way to achieve this is to store the nonce alongside the encrypted
  71. // message. Above, we stored the nonce in the first 24 bytes of the
  72. // encrypted text.
  73. var decryptNonce [24]byte
  74. copy(decryptNonce[:], encrypted[:24])
  75. decrypted, ok := box.OpenAfterPrecomputation(nil, encrypted[24:], &decryptNonce, &sharedDecryptKey)
  76. if !ok {
  77. panic("decryption error")
  78. }
  79. fmt.Println(string(decrypted))
  80. // Output: A fellow of infinite jest, of most excellent fancy
  81. }