示例#1
0
func BoxOpen(messageOut []byte, cypherText []byte, nonce, pk, sk []byte) int {
	checkSize(messageOut, len(cypherText), "message output")
	checkSize(nonce, BoxNonceBytes(), "nonce")
	checkSize(pk, BoxPublicKeyBytes(), "public key")
	checkSize(sk, BoxSecretKeyBytes(), "secret key")

	return int(C.crypto_box_open(
		(*C.uchar)(&messageOut[0]),
		(*C.uchar)(&cypherText[0]), (C.ulonglong)(len(cypherText)),
		(*C.uchar)(&nonce[0]),
		(*C.uchar)(&pk[0]),
		(*C.uchar)(&sk[0])))
}
func Unbox(box []byte, nonce []byte, pk PublicKey, sk SecretKey) ([]byte, error) {
	result := make([]byte, C.crypto_box_BOXZEROBYTES+len(box))

	copy(result[C.crypto_box_BOXZEROBYTES:], box)

	code := C.crypto_box_open(array(result), array(result), C.ulonglong(len(result)), array(nonce), array(pk), array(sk))

	if code == 0 {
		return result[C.crypto_box_ZEROBYTES:], nil
	} else {
		return nil, ErrBoxOpen
	}
}
示例#3
0
func CryptoBoxOpen(c []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
	support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
	support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
	support.CheckSize(sk, CryptoBoxPublicKeyBytes(), "secret key")
	m := make([]byte, len(c))
	exit := int(C.crypto_box_open(
		(*C.uchar)(&m[0]),
		(*C.uchar)(&c[0]),
		(C.ulonglong)(len(c)),
		(*C.uchar)(&n[0]),
		(*C.uchar)(&pk[0]),
		(*C.uchar)(&sk[0])))

	return m, exit
}