You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.6 KiB
Go

7 years ago
package gnutls
/*
#include <stdlib.h>
#include "_gnutls.h"
*/
import "C"
import (
"fmt"
)
6 years ago
// HashType hash type
type HashType int
7 years ago
const (
6 years ago
GNUTLS_HASH_MD5 HashType = 2
GNUTLS_HASH_SHA1 HashType = 3
GNUTLS_HASH_MD2 HashType = 5
GNUTLS_HASH_SHA256 HashType = 6
GNUTLS_HASH_SHA384 HashType = 7
GNUTLS_HASH_SHA512 HashType = 8
GNUTLS_HASH_SHA224 HashType = 9
7 years ago
)
// Hash hash struct
type Hash struct {
hash C.gnutls_hash_hd_t
6 years ago
t HashType
hashLen C.int
7 years ago
}
// NewHash new hash struct
6 years ago
func NewHash(t HashType) *Hash {
7 years ago
h := C.new_hash(C.int(t))
hashOutLen := GetHashOutputLen(t)
return &Hash{h, t, C.int(hashOutLen)}
7 years ago
}
// Write write data to hash context
func (h *Hash) Write(buf []byte) error {
dataLen := len(buf)
cbuf := C.CBytes(buf)
defer C.free(cbuf)
ret := C.gnutls_hash(h.hash, cbuf, C.size_t(dataLen))
if int(ret) < 0 {
return fmt.Errorf("hash failed: %s", C.GoString(C.gnutls_strerror(ret)))
}
return nil
}
// Sum get hash result
func (h *Hash) Sum(buf []byte) []byte {
if buf != nil {
h.Write(buf)
}
dstBuf := C.malloc(C.size_t(h.hashLen))
7 years ago
defer C.free(dstBuf)
C.gnutls_hash_output(h.hash, dstBuf)
gobuf := C.GoBytes(dstBuf, h.hashLen)
7 years ago
return gobuf
}
// Close destroy hash context
func (h *Hash) Close() error {
C.gnutls_hash_deinit(h.hash, nil)
return nil
}
// GetHashOutputLen get the hash algorithm output length
6 years ago
//
// example GNUTLS_MD5 is 16
6 years ago
func GetHashOutputLen(t HashType) int {
return int(C.gnutls_hash_get_len(C.gnutls_digest_algorithm_t(t)))
}