예제 #1
0
func GenericHash(hashOut []byte, message []byte, key []byte) int {
	checkHashOutSize(hashOut)
	checkKeySize(key)
	if key == []byte(nil) {
		return int(C.crypto_generichash(
			(*C.uchar)(&hashOut[0]), (C.size_t)(len(hashOut)),
			(*C.uchar)(&message[0]), (C.ulonglong)(len(message)),
			nil, (C.size_t)(0)))
	}
	return int(C.crypto_generichash(
		(*C.uchar)(&hashOut[0]), (C.size_t)(len(hashOut)),
		(*C.uchar)(&message[0]), (C.ulonglong)(len(message)),
		(*C.uchar)(&key[0]), (C.size_t)(len(key))))
}
예제 #2
0
// I took care of the typedef confusions. This should work okay.
func CryptoGenericHash(outlen int, in []byte, key []byte) ([]byte, int) {
	support.CheckSizeInRange(outlen, CryptoGenericHashBytesMin(), CryptoGenericHashBytesMax(), "out")
	support.CheckSizeInRange(len(key), CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key")
	out := make([]byte, outlen)
	exit := int(C.crypto_generichash(
		(*C.uchar)(&out[0]),
		(C.size_t)(outlen),
		(*C.uchar)(&in[0]),
		(C.ulonglong)(len(in)),
		(*C.uchar)(&key[0]),
		(C.size_t)(len(key))))

	return out, exit
}
예제 #3
0
// SecureHash uses the Blake2b algorithm to generate a 256-bit (32-byte)
// hash of a message with an optional key. The key parameter can be nil if
// normal hashing, instead of authenticated hashing, is wanted.
func SecureHash(message []byte, key []byte) []byte {
	out := make([]byte, 32)
	var keyptr *C.uchar
	var msgptr *C.uchar
	var keylen C.size_t
	var msglen C.ulonglong
	if message == nil || len(message) == 0 {
		msgptr = nil
	} else {
		msgptr = (*C.uchar)(&message[0])
		msglen = C.ulonglong(len(message))
	}
	if key == nil || len(key) == 0 {
		keyptr = nil
	} else {
		keyptr = (*C.uchar)(&key[0])
		keylen = C.size_t(len(key))
	}
	C.crypto_generichash((*C.uchar)(&out[0]), 32,
		msgptr, msglen,
		keyptr, keylen)
	return out
}