Skip to content

Commit

Permalink
space monkey internal commit export
Browse files Browse the repository at this point in the history
[katamari commit: 05c97fb8e733433a63dcedaa7408c63beedd286f]
  • Loading branch information
jtolio committed Mar 24, 2014
1 parent b42853e commit 76a5e05
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
39 changes: 39 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 5 deletions sha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 76a5e05

Please sign in to comment.