Example #1
0
// Encrypt generates a random nonce and encrypts the input using
// NaCl's secretbox package. The nonce is prepended to the ciphertext.
func Encrypt(key *[KeySize]byte, in []byte) ([]byte, bool) {
	var out = make([]byte, nonceSize)
	nonce := util.NewNonce()
	if nonce == nil {
		return nil, false
	}

	copy(out, nonce[:])
	out = secretbox.Seal(out, in, nonce, key)
	return out, true
}
Example #2
0
// encrypt the message without encoding it
func testEncryptBare(pub *PublicKey, message []byte) (out []byte, ok bool) {
	if !pub.Valid() {
		return nil, false
	}

	prng := util.PRNG()
	epub, epriv, err := box.GenerateKey(prng)
	if err != nil {
		return nil, false
	}

	out = epub[:]
	nonce := util.NewNonce()
	out = append(out, nonce[:]...)

	out = box.Seal(out, message, nonce, pub.E, epriv)
	ok = true
	return
}
Example #3
0
// write the message two times to PT
func testEncryptTwo(pub *PublicKey, message []byte) (out []byte, ok bool) {
	if !pub.Valid() {
		return nil, false
	}

	prng := util.PRNG()
	epub, epriv, err := box.GenerateKey(prng)
	if err != nil {
		return nil, false
	}

	enc := &tlv.Encoder{}
	enc.Encode(message)
	enc.Encode(message)

	out = epub[:]
	nonce := util.NewNonce()
	out = append(out, nonce[:]...)

	out = box.Seal(out, enc.Bytes(), nonce, pub.E, epriv)
	ok = true
	return
}