-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
124 lines (103 loc) · 2.75 KB
/
options.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2023 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package rsa
import (
"crypto"
"crypto/rand"
"crypto/sha256"
"hash"
"io"
)
// KeyConfig stores all configurations of key.
type KeyConfig struct {
privateKeyEncoder PrivateKeyEncoder
privateKeyDecoder PrivateKeyDecoder
publicKeyEncoder PublicKeyEncoder
publicKeyDecoder PublicKeyDecoder
}
// fromKeyOptions returns a key config constructed from key options.
func fromKeyOptions(opts []KeyOption) *KeyConfig {
cfg := &KeyConfig{
privateKeyEncoder: X509.PKCS1PrivateKeyEncoder,
privateKeyDecoder: X509.PKCS1PrivateKeyDecoder,
publicKeyEncoder: X509.PKIXPublicKeyEncoder,
publicKeyDecoder: X509.PKIXPublicKeyDecoder,
}
for _, opt := range opts {
opt.ApplyTo(cfg)
}
return cfg
}
// KeyOption is an option for key config.
type KeyOption func(cfg *KeyConfig)
// ApplyTo applies key option to key config.
func (ko KeyOption) ApplyTo(cfg *KeyConfig) {
ko(cfg)
}
// WithPrivateKeyEncoder sets private key encoder to cfg.
func WithPrivateKeyEncoder(encoder PrivateKeyEncoder) KeyOption {
return func(cfg *KeyConfig) {
cfg.privateKeyEncoder = encoder
}
}
// WithPrivateKeyDecoder sets private key decoder to cfg.
func WithPrivateKeyDecoder(decoder PrivateKeyDecoder) KeyOption {
return func(cfg *KeyConfig) {
cfg.privateKeyDecoder = decoder
}
}
// WithPublicKeyEncoder sets public key encoder to cfg.
func WithPublicKeyEncoder(encoder PublicKeyEncoder) KeyOption {
return func(cfg *KeyConfig) {
cfg.publicKeyEncoder = encoder
}
}
// WithPublicKeyDecoder sets public key decoder to cfg.
func WithPublicKeyDecoder(decoder PublicKeyDecoder) KeyOption {
return func(cfg *KeyConfig) {
cfg.publicKeyDecoder = decoder
}
}
// Config stores all configurations used by encrypting/decrypting/signing/verifying.
type Config struct {
random io.Reader
hash hash.Hash
cryptoHash crypto.Hash
}
// fromOptions returns a config constructed from options.
func fromOptions(opts []Option) *Config {
cfg := &Config{
random: rand.Reader,
hash: sha256.New(),
cryptoHash: crypto.SHA256,
}
for _, opt := range opts {
opt.ApplyTo(cfg)
}
return cfg
}
// Option is an option for config.
type Option func(cfg *Config)
// ApplyTo applies option to config.
func (o Option) ApplyTo(cfg *Config) {
o(cfg)
}
// WithRandom sets random to cfg.
func WithRandom(random io.Reader) Option {
return func(cfg *Config) {
cfg.random = random
}
}
// WithHash sets hash to cfg.
func WithHash(hash hash.Hash) Option {
return func(cfg *Config) {
cfg.hash = hash
}
}
// WithCryptoHash sets crypto hash to cfg.
func WithCryptoHash(hash crypto.Hash) Option {
return func(cfg *Config) {
cfg.cryptoHash = hash
}
}