...
Package secretbox
Package secretbox encrypts and authenticates small messages.
Secretbox uses XSalsa20 and Poly1305 to encrypt and authenticate messages with
secret-key cryptography. The length of messages is not hidden.
It is the caller's responsibility to ensure the uniqueness of nonces—for
example, by using nonce 1 for the first message, nonce 2 for the second
message, etc. Nonces are long enough that randomly generated nonces have
negligible risk of collision.
This package is interoperable with NaCl: https://nacl.cr.yp.to/secretbox.html.
▾ Example
Code:
secretKeyBytes, err := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
if err != nil {
panic(err)
}
var secretKey [32]byte
copy(secretKey[:], secretKeyBytes)
var nonce [24]byte
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
panic(err)
}
encrypted := secretbox.Seal(nonce[:], []byte("hello world"), &nonce, &secretKey)
var decryptNonce [24]byte
copy(decryptNonce[:], encrypted[:24])
decrypted, ok := secretbox.Open(nil, encrypted[24:], &decryptNonce, &secretKey)
if !ok {
panic("decryption error")
}
fmt.Println(string(decrypted))
Output:
hello world
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls—perhaps dynamically.
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
Constants
Overhead is the number of bytes of overhead when boxing a message.
const Overhead = poly1305.TagSize
func Open(out []byte, box []byte, nonce *[24]byte, key *[32]byte) ([]byte, bool)
Open authenticates and decrypts a box produced by Seal and appends the
message to out, which must not overlap box. The output will be Overhead
bytes smaller than box.
func Seal(out, message []byte, nonce *[24]byte, key *[32]byte) []byte
Seal appends an encrypted and authenticated copy of message to out, which
must not overlap message. The key and nonce pair must be unique for each
distinct message and the output will be Overhead bytes longer than message.