add hash output length get function

master
fangdingjun 6 years ago
parent 2e02d7a93c
commit 11f1e5c1a6

@ -10,25 +10,27 @@ import (
)
const (
GNUTLS_MD5 = 2
GNUTLS_SHA1 = 3
GNUTLS_MD2 = 5
GNUTLS_SHA256 = 6
GNUTLS_SHA384 = 7
GNUTLS_SHA512 = 8
GNUTLS_SHA224 = 9
GNUTLS_HASH_MD5 = 2
GNUTLS_HASH_SHA1 = 3
GNUTLS_HASH_MD2 = 5
GNUTLS_HASH_SHA256 = 6
GNUTLS_HASH_SHA384 = 7
GNUTLS_HASH_SHA512 = 8
GNUTLS_HASH_SHA224 = 9
)
// Hash hash struct
type Hash struct {
hash C.gnutls_hash_hd_t
t int
hash C.gnutls_hash_hd_t
t int
hashLen C.int
}
// NewHash new hash struct
func NewHash(t int) *Hash {
h := C.new_hash(C.int(t))
return &Hash{h, t}
hashOutLen := GetHashOutputLen(t)
return &Hash{h, t, C.int(hashOutLen)}
}
// Write write data to hash context
@ -51,14 +53,12 @@ func (h *Hash) Sum(buf []byte) []byte {
h.Write(buf)
}
hashOutLen := C.get_hash_len(C.int(h.t))
dstBuf := C.malloc(C.size_t(hashOutLen))
dstBuf := C.malloc(C.size_t(h.hashLen))
defer C.free(dstBuf)
C.gnutls_hash_output(h.hash, dstBuf)
gobuf := C.GoBytes(dstBuf, hashOutLen)
gobuf := C.GoBytes(dstBuf, h.hashLen)
return gobuf
}
@ -68,3 +68,9 @@ func (h *Hash) Close() error {
C.gnutls_hash_deinit(h.hash, nil)
return nil
}
// GetHashOutputLen get the hash algorithm output length
// example GNUTLS_MD5 is 16
func GetHashOutputLen(t int) int {
return int(C.gnutls_hash_get_len(C.gnutls_digest_algorithm_t(t)))
}

Loading…
Cancel
Save