// if the returned array is empty, then make a call to sslerr.Error() func (d *digest) Sum(b []byte) []byte { buf := make([]C.uchar, d.Size()) // make a copy of the pointer, so our context does not get freed. // this allows further writes. ctxTmp := C.SHA256_CTX(d.ctx) if d.is224 { if C.SHA224_Final(&buf[0], &ctxTmp) != 1 { return make([]byte, 0) } } else { if C.SHA256_Final(&buf[0], &ctxTmp) != 1 { return make([]byte, 0) } } var result []byte if b != nil { result = make([]byte, 0) } else { result = b } for _, value := range buf { result = append(result, byte(value)) } return result }
func sum256(data []byte) []byte { var hash = make([]byte, 65) var sha256 C.SHA256_CTX C.SHA256_Init(&sha256) C.SHA256_Update(&sha256, unsafe.Pointer(&data[0]), C.size_t(len(data))) C.SHA256_Final((*C.uchar)(&hash[0]), &sha256) return hash[:64] }
func (h *sha256) Sum(b []byte) []byte { c := h.ctx d := make([]byte, C.SHA256_DIGEST_LENGTH) if C.SHA256_Final((*C.uchar)(unsafe.Pointer(&d[0])), &c) == 0 { panic("SHA256_Final failed") } return append(b, d...) }
func (self *SHA256Hash) Sum(b []byte) []byte { digest := make([]C.uchar, self.Size()) if C.SHA256_Final(&digest[0], &self.sha) != 1 { panic("couldn't finalize digest") } var result []byte if b != nil { result = make([]byte, 0) } else { result = b } for _, value := range digest { result = append(result, byte(value)) } return result }
func (self *SHA256Hash) Sum(b []byte) []byte { digest := make([]C.uchar, self.Size()) // make a copy of the pointer, so our context does not get freed. // this allows further writes. // TODO perhaps we should think about runtime.SetFinalizer to free the context? s_tmp := C.SHA256_CTX(self.sha) if C.SHA256_Final(&digest[0], &s_tmp) != 1 { panic("couldn't finalize digest") } var result []byte if b != nil { result = make([]byte, 0) } else { result = b } for _, value := range digest { result = append(result, byte(value)) } return result }