Documentation ¶
Index ¶
- Constants
- func DecryptWriter(w io.Writer, key, nonce []byte) (io.WriteCloser, error)
- func EncryptWriter(w io.Writer, key, nonce []byte) (io.WriteCloser, error)
- func NewCipher(key []byte) (cipher.AEAD, error)
- func NewIETFCipher(key []byte) (cipher.AEAD, error)
- func NewXCipher(key []byte) (cipher.AEAD, error)
Examples ¶
Constants ¶
const KeySize = 32
KeySize is the size of the key used by this AEAD, in bytes.
Variables ¶
This section is empty.
Functions ¶
func DecryptWriter ¶
DecryptWriter wraps an io.Writer and returns an io.WriteCloser which decrypts and checks authenticity of all input passed into it with the given key and nonce. The Close function of the returned io.WriteCloser must be called to finish the decryption successfully. If the Close function returns a non-nil error the decryption failed - for example because of an incorrect authentication tag. So the returned error of Close MUST be checked!
If the Write or Close function of the io.WriteCloser returns a non-nil error the hole decryption process cannot succeed and must be restarted. The length of the nonce determines which cipher is used:
- 8 byte: ChaCha20Poly1305 with 64 bit nonces
- 12 byte: ChaCha20Poly1305 with 96 bit nonces (used in TLS)
- 24 byte: XChaCha20Poly1305 with 192 bit nonces
Example ¶
// Create a secret key - can also be generated from a passwort or ... key := make([]byte, 32) if _, err := io.ReadFull(rand.Reader, key); err != nil { panic(fmt.Sprintf("Failed to create random encryption key: %v", err)) } // Set the nonce, in this case we encrypt the first message so we set it to 1. // Be aware that the nonce must be unique for every message under the same key. nonce := make([]byte, 8) binary.LittleEndian.PutUint64(nonce, 1) // We first encrypt a stream. mem := bytes.NewBuffer(nil) encryptedWriter, err := EncryptWriter(mem, key, nonce) if err != nil { panic(fmt.Sprintf("Cannot create encrypted writer: %v", err)) } msg := []byte("Nobody should see this - ever!") encryptedWriter.Write(msg) if err := encryptedWriter.Close(); err != nil { panic(fmt.Sprintf("Failed to finish encryption: %v", err)) } ciphertext := mem.Bytes() mem = bytes.NewBuffer(nil) // So now we want to decrypt the message decryptedWriter, err := DecryptWriter(mem, key, nonce) if err != nil { panic(fmt.Sprintf("Cannot create decrypted writer: %v", err)) } decryptedWriter.Write(ciphertext) // Close finishes the decryption. Close MUST be called to successfully decrypt a // ciphertext. Further Close returns a non-nil error if the decryption failed - e.g. if // the authentication failed - so we MUST invoke Close and MUST check if the returned error // is nil. Otherwise the decryption will fail / was not successfull. if err := decryptedWriter.Close(); err != nil { panic(fmt.Sprintf("Failed to finish decryption: %v", err)) } fmt.Println(string(mem.Bytes()))
Output: Nobody should see this - ever!
func EncryptWriter ¶
EncryptWriter wraps an io.Writer and returns an io.WriteCloser which encrypts and authenticates all input passed into it with the given key and nonce. The Close function of the returned io.WriteCloser must be called to finish the encryption successfully.
If the Write or Close function of the io.WriteCloser returns a non-nil error the hole encryption process cannot succeed and must be restarted. The length of the nonce determines which cipher is used:
- 8 byte: ChaCha20Poly1305 with 64 bit nonces
- 12 byte: ChaCha20Poly1305 with 96 bit nonces (used in TLS)
- 24 byte: XChaCha20Poly1305 with 192 bit nonces
Example ¶
// we write to memory, real code may write to a file, network connection or ... mem := bytes.NewBuffer(nil) // Create a secret key - can also be generated from a passwort or ... key := make([]byte, 32) if _, err := io.ReadFull(rand.Reader, key); err != nil { panic(fmt.Sprintf("Failed to create random encryption key: %v", err)) } // Set the nonce, in this case we encrypt the first message so we set it to 1. // Be aware that the nonce must be unique for every message under the same key. nonce := make([]byte, 8) binary.LittleEndian.PutUint64(nonce, 1) encryptedWriter, err := EncryptWriter(mem, key, nonce) if err != nil { panic(fmt.Sprintf("Cannot create encrypted writer: %v", err)) } // Close finishes the encryption, so it MUST be called! We ensure this through defer. defer func() { if err := encryptedWriter.Close(); err != nil { panic(fmt.Sprintf("Failed to finish encryption: %v", err)) } }() msg := []byte("Nobody should see this - ever!") // encrypt the message encryptedWriter.Write(msg)
Output:
func NewCipher ¶
NewCipher returns a cipher.AEAD implementing the ChaCha20Poly1305 construction specified in RFC 7539 with a 128 bit auth. tag.
func NewIETFCipher ¶
NewIETFCipher returns a cipher.AEAD implementing the ChaCha20Poly1305 construction specified in RFC 7539 with a 128 bit auth. tag.
Types ¶
This section is empty.