// Copyright 2018 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package chacha20poly1305import ()typexchacha20poly1305struct {key [KeySize]byte}// NewX returns a XChaCha20-Poly1305 AEAD that uses the given 256-bit key.//// XChaCha20-Poly1305 is a ChaCha20-Poly1305 variant that takes a longer nonce,// suitable to be generated randomly without risk of collisions. It should be// preferred when nonce uniqueness cannot be trivially ensured, or whenever// nonces are randomly generated.func ( []byte) (cipher.AEAD, error) {iflen() != KeySize {returnnil, errors.New("chacha20poly1305: bad key length") } := new(xchacha20poly1305)copy(.key[:], )return , nil}func (*xchacha20poly1305) () int {returnNonceSizeX}func (*xchacha20poly1305) () int {returnOverhead}func ( *xchacha20poly1305) (, , , []byte) []byte {iflen() != NonceSizeX {panic("chacha20poly1305: bad nonce length passed to Seal") }// XChaCha20-Poly1305 technically supports a 64-bit counter, so there is no // size limit. However, since we reuse the ChaCha20-Poly1305 implementation, // the second half of the counter is not available. This is unlikely to be // an issue because the cipher.AEAD API requires the entire message to be in // memory, and the counter overflows at 256 GB.ifuint64(len()) > (1<<38)-64 {panic("chacha20poly1305: plaintext too large") } := new(chacha20poly1305) , := chacha20.HChaCha20(.key[:], [0:16])copy(.key[:], )// The first 4 bytes of the final nonce are unused counter space. := make([]byte, NonceSize)copy([4:12], [16:24])return .seal(, [:], , )}func ( *xchacha20poly1305) (, , , []byte) ([]byte, error) {iflen() != NonceSizeX {panic("chacha20poly1305: bad nonce length passed to Open") }iflen() < 16 {returnnil, errOpen }ifuint64(len()) > (1<<38)-48 {panic("chacha20poly1305: ciphertext too large") } := new(chacha20poly1305) , := chacha20.HChaCha20(.key[:], [0:16])copy(.key[:], )// The first 4 bytes of the final nonce are unused counter space. := make([]byte, NonceSize)copy([4:12], [16:24])return .open(, [:], , )}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.