-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcypto.go
More file actions
59 lines (54 loc) · 1.28 KB
/
cypto.go
File metadata and controls
59 lines (54 loc) · 1.28 KB
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
package gibero
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"errors"
)
func DecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) ([]byte, error) {
hash := sha1.New()
plaintext, err := rsa.DecryptOAEP(hash, rand.Reader, priv, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
func BytesToPrivateKey(raw []byte, password []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(raw)
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
if enc {
b, err = x509.DecryptPEMBlock(block, password)
if err != nil {
return nil, err
}
}
val, err := x509.ParsePKCS8PrivateKey(b)
if err != nil {
return nil, err
}
key, ok := val.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("known_key")
}
return key, nil
}
func BytesToPublicKey(raw []byte) (*rsa.PublicKey, error) {
block, _ := pem.Decode(raw)
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
return publicKeyInterface.(*rsa.PublicKey), nil
}
func EncryptWithPublicKey(plainText []byte, publickey *rsa.PublicKey) []byte {
sha1 := sha1.New()
cipherText, err := rsa.EncryptOAEP(sha1, rand.Reader, publickey, plainText, nil)
if err != nil {
panic(err)
}
return cipherText
}