-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa.go
40 lines (33 loc) · 982 Bytes
/
rsa.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
package jwk
import (
"crypto/rsa"
"fmt"
"filippo.io/age/agessh"
"golang.org/x/crypto/ssh"
)
func EncryptRSA(pubKey *rsa.PublicKey, plaintext []byte) ([]byte, error) {
sshPubKey, err := ssh.NewPublicKey(pubKey)
if err != nil {
return nil, fmt.Errorf("jwk-rsa: failed to convert public key to ssh format: %w", err)
}
recipient, err := agessh.NewRSARecipient(sshPubKey)
if err != nil {
return nil, fmt.Errorf("jwk-rsa: failed to create recipient: %w", err)
}
ciphertext, err := ageEncrypt(recipient, plaintext)
if err != nil {
return nil, fmt.Errorf("jwk-rsa: %w", err)
}
return ciphertext, nil
}
func DecryptRSA(privKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error) {
identity, err := agessh.NewRSAIdentity(privKey)
if err != nil {
return nil, fmt.Errorf("jwk-rsa: failed to create identity: %w", err)
}
plaintext, err := ageDecrypt(identity, ciphertext)
if err != nil {
return nil, fmt.Errorf("jwk-rsa: %w", err)
}
return plaintext, nil
}