-
Notifications
You must be signed in to change notification settings - Fork 4
/
key.go
executable file
·51 lines (42 loc) · 1.01 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cryptopro
//#include "common.h"
import "C"
import "github.com/pkg/errors"
type KeyFlag C.DWORD
type KeyPairId C.DWORD
var (
ErrGetKey = errors.New("error getting key for container")
ErrCreateKey = errors.New("error creating key for container")
ErrCloseKey = errors.New("error close key")
)
const (
KeyArchivable KeyFlag = C.CRYPT_ARCHIVABLE
KeyExportable KeyFlag = C.CRYPT_EXPORTABLE
)
const (
AtKeyExchange KeyPairId = C.AT_KEYEXCHANGE
AtSignature KeyPairId = C.AT_SIGNATURE
)
type Key struct {
hKey C.HCRYPTKEY
}
func (ctx Ctx) Key(at KeyPairId) (Key, error) {
res := Key{}
if C.CryptGetUserKey(ctx.hProv, C.DWORD(at), &res.hKey) == 0 {
return res, ErrGetKey
}
return res, nil
}
func (ctx Ctx) GenKey(at KeyPairId, flags KeyFlag) (Key, error) {
res := Key{}
if C.CryptGenKey(ctx.hProv, C.ALG_ID(at), C.DWORD(flags), &res.hKey) == 0 {
return res, ErrCreateKey
}
return res, nil
}
func (key Key) Close() error {
if C.CryptDestroyKey(key.hKey) == 0 {
return ErrCloseKey
}
return nil
}