-
Notifications
You must be signed in to change notification settings - Fork 0
/
public.go
72 lines (57 loc) · 2.01 KB
/
public.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
// Copyright 2022 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/rsa"
"github.com/FishGoddess/cryptox"
)
// PublicKey is the public key of rsa.
type PublicKey struct {
key *rsa.PublicKey
keyBytes cryptox.Bytes
}
// newPublicKey returns a public key.
func newPublicKey(key *rsa.PublicKey, keyBytes cryptox.Bytes) PublicKey {
return PublicKey{key: key, keyBytes: keyBytes}
}
// Key returns the key of pk.
func (pk PublicKey) Key() *rsa.PublicKey {
return pk.key
}
// Bytes returns the bytes of pk.
func (pk PublicKey) Bytes() cryptox.Bytes {
return pk.keyBytes
}
// EqualsTo returns if pk equals to privateKey.
func (pk PublicKey) EqualsTo(publicKey PublicKey) bool {
return pk.key.Equal(publicKey.key)
}
// String returns the formatted string of pk.
func (pk PublicKey) String() string {
return pk.keyBytes.String()
}
// EncryptPKCS1v15 encrypts msg with pkcs1 v15.
func (pk PublicKey) EncryptPKCS1v15(msg cryptox.Bytes, opts ...Option) (cryptox.Bytes, error) {
cfg := fromOptions(opts...)
return rsa.EncryptPKCS1v15(cfg.random, pk.key, msg)
}
// EncryptOAEP encrypts msg with oaep.
func (pk PublicKey) EncryptOAEP(msg cryptox.Bytes, label cryptox.Bytes, opts ...Option) (cryptox.Bytes, error) {
cfg := fromOptions(opts...)
return rsa.EncryptOAEP(cfg.hash, cfg.random, pk.key, msg, label)
}
// VerifyPKCS1v15 verifies signature with pkcs1 v15.
func (pk PublicKey) VerifyPKCS1v15(hashed cryptox.Bytes, signature cryptox.Bytes, opts ...Option) error {
cfg := fromOptions(opts...)
return rsa.VerifyPKCS1v15(pk.key, cfg.cryptoHash, hashed, signature)
}
// VerifyPSS verifies signature with pss.
func (pk PublicKey) VerifyPSS(digest cryptox.Bytes, signature cryptox.Bytes, saltLength int, opts ...Option) error {
cfg := fromOptions(opts...)
pssOpts := &rsa.PSSOptions{
Hash: cfg.cryptoHash,
SaltLength: saltLength,
}
return rsa.VerifyPSS(pk.key, cfg.cryptoHash, digest, signature, pssOpts)
}