mirror of
https://github.com/libp2p/go-openssl.git
synced 2025-01-14 02:30:08 +08:00
space monkey internal commit export
[katamari commit: 05c97fb8e733433a63dcedaa7408c63beedd286f]
This commit is contained in:
parent
b42853e994
commit
76a5e0584d
39
engine.go
Normal file
39
engine.go
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2014 Space Monkey, Inc.
|
||||
// +build cgo
|
||||
|
||||
package openssl
|
||||
|
||||
/*
|
||||
#include "openssl/engine.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
e *C.ENGINE
|
||||
}
|
||||
|
||||
func EngineById(name string) (*Engine, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
e := &Engine{
|
||||
e: C.ENGINE_by_id(cname),
|
||||
}
|
||||
if e.e == nil {
|
||||
return nil, fmt.Errorf("engine %s missing", name)
|
||||
}
|
||||
if C.ENGINE_init(e.e) == 0 {
|
||||
C.ENGINE_free(e.e)
|
||||
return nil, fmt.Errorf("engine %s not initialized", name)
|
||||
}
|
||||
runtime.SetFinalizer(e, func(e *Engine) {
|
||||
C.ENGINE_finish(e.e)
|
||||
C.ENGINE_free(e.e)
|
||||
})
|
||||
return e, nil
|
||||
}
|
20
sha1.go
20
sha1.go
@ -20,13 +20,16 @@ import (
|
||||
)
|
||||
|
||||
type SHA1Hash struct {
|
||||
ctx C.EVP_MD_CTX
|
||||
ctx C.EVP_MD_CTX
|
||||
engine *Engine
|
||||
}
|
||||
|
||||
func NewSHA1Hash() (*SHA1Hash, error) {
|
||||
hash := new(SHA1Hash)
|
||||
func NewSHA1Hash() (*SHA1Hash, error) { return NewSHA1HashWithEngine(nil) }
|
||||
|
||||
func NewSHA1HashWithEngine(e *Engine) (*SHA1Hash, error) {
|
||||
hash := &SHA1Hash{engine: e}
|
||||
C.EVP_MD_CTX_init(&hash.ctx)
|
||||
runtime.SetFinalizer(hash, func(h *SHA1Hash) { h.Close() })
|
||||
runtime.SetFinalizer(hash, func(hash *SHA1Hash) { hash.Close() })
|
||||
if err := hash.Reset(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -37,8 +40,15 @@ func (s *SHA1Hash) Close() {
|
||||
C.EVP_MD_CTX_cleanup(&s.ctx)
|
||||
}
|
||||
|
||||
func engineRef(e *Engine) *C.ENGINE {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.e
|
||||
}
|
||||
|
||||
func (s *SHA1Hash) Reset() error {
|
||||
if 1 != C.EVP_DigestInit_ex(&s.ctx, C.EVP_sha1(), nil) {
|
||||
if 1 != C.EVP_DigestInit_ex(&s.ctx, C.EVP_sha1(), engineRef(s.engine)) {
|
||||
return errors.New("openssl: sha1: cannot init digest ctx")
|
||||
}
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user