Exemple #1
0
// open an encrypted box
func CryptoBoxOpen(box, nounce, sk, pk []byte) ([]byte, error) {
	boxbuff := NewBuffer(box)
	defer boxbuff.Free()

	// check sizes
	if len(pk) != int(C.crypto_box_publickeybytes()) {
		err := errors.New("len(pk) != crypto_box_publickey_bytes")
		return nil, err
	}
	if len(sk) != int(C.crypto_box_secretkeybytes()) {
		err := errors.New("len(sk) != crypto_box_secretkey_bytes")
		return nil, err
	}
	if len(nounce) != int(C.crypto_box_macbytes()) {
		err := errors.New("len(nounce) != crypto_box_macbytes()")
		return nil, err
	}

	pkbuff := NewBuffer(pk)
	defer pkbuff.Free()
	skbuff := NewBuffer(sk)
	defer skbuff.Free()
	nouncebuff := NewBuffer(nounce)
	defer nouncebuff.Free()
	resultbuff := malloc(boxbuff.size - nouncebuff.size)
	defer resultbuff.Free()

	// decrypt
	res := C.crypto_box_open_easy(resultbuff.uchar(), boxbuff.uchar(), C.ulonglong(boxbuff.size), nouncebuff.uchar(), pkbuff.uchar(), skbuff.uchar())
	if res != 0 {
		return nil, errors.New("crypto_box_open_easy failed")
	}
	// return result
	return resultbuff.Bytes(), nil
}
Exemple #2
0
func BoxOpenEasy(messageOut []byte, cypherText []byte, nonce, pk, sk []byte) int {
	checkSize(messageOut, BoxMacBytes()+len(cypherText), "message output")
	checkSize(nonce, BoxNonceBytes(), "nonce")
	checkSize(pk, BoxPublicKeyBytes(), "public key")
	checkSize(sk, BoxSecretKeyBytes(), "secret key")

	return int(C.crypto_box_open_easy(
		(*C.uchar)(&messageOut[0]),
		(*C.uchar)(&cypherText[0]), (C.ulonglong)(len(cypherText)),
		(*C.uchar)(&nonce[0]),
		(*C.uchar)(&pk[0]),
		(*C.uchar)(&sk[0])))
}