/
keys.go
78 lines (66 loc) · 1.7 KB
/
keys.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package keys
import (
"crypto/rsa"
"math/big"
"github.com/didiercrunch/filou/shared"
)
const (
DEFAULT_BLOCK_CYPHER = "AES"
DEFAULT_BLOCK_CYPHER_MODE = "CBC"
DEFAULT_HASH_FUNCTION = "SHA256"
)
type PrivateKey struct {
Version string
Type string // rsa, el gamal,...
Key *RSAPrivateKey
Accept *AcceptMethods
}
type PublicKey struct {
Type string // rsa, el gamal,...
Version string
Key *RSAPublicKey
Accept *AcceptMethods
}
type RSAPublicKey struct {
N *big.Int
E int
}
type RSAPrivateKey struct {
N *big.Int `json:"n,omitempty"`
P *big.Int `json:"p,omitempty"`
Q *big.Int `json:"q,omitempty"`
D *big.Int `json:"d,omitempty"`
E int `json:"e,omitempty"`
}
type AcceptMethods struct {
BlocCypher string
BlockCypherMode string
HashFunction string
}
func GetDefaultRSAPublicKey(publicKey *rsa.PublicKey) *PublicKey {
key := &RSAPublicKey{publicKey.N, publicKey.E}
accept := &AcceptMethods{
DEFAULT_BLOCK_CYPHER,
DEFAULT_BLOCK_CYPHER_MODE,
DEFAULT_HASH_FUNCTION,
}
return &PublicKey{Type: "RSA", Version: shared.VERSION, Key: key, Accept: accept}
}
func NewRSAPublicKey(key *rsa.PublicKey) *RSAPublicKey {
return &RSAPublicKey{key.N, key.E}
}
func NewRSAPrivateKey(key *rsa.PrivateKey) *RSAPrivateKey {
return &RSAPrivateKey{key.N, key.Primes[0], key.Primes[1], key.D, key.E}
}
func GetDefaultRSAPrivateKey(privateKey *rsa.PrivateKey) (*PrivateKey, error) {
if err := privateKey.Validate(); err != nil {
return nil, err
}
key := NewRSAPrivateKey(privateKey)
accept := &AcceptMethods{
DEFAULT_BLOCK_CYPHER,
DEFAULT_BLOCK_CYPHER_MODE,
DEFAULT_HASH_FUNCTION,
}
return &PrivateKey{Type: "RSA", Version: shared.VERSION, Key: key, Accept: accept}, nil
}