-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
42 lines (35 loc) · 1000 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
41
42
//Package ipin implements IPIN encryption as per EBS
package ipin
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"fmt"
)
// TODO #2 make this main program, a package
//Encrypt given a public key and pin and UUID, encrypt encrypts
// to EBS compatible RSA payload
func Encrypt(pubkey string, pin string, uuid string) (string, error) {
block, err := base64.StdEncoding.DecodeString(pubkey)
if err != nil {
return "", err
}
pub, err := x509.ParsePKIXPublicKey(block)
if err != nil {
return "", err
}
rsaPub, _ := pub.(*rsa.PublicKey)
//fmt.Printf("The key is: %v, its type is %T", rsaPub, rsaPub)
// do the encryption
msg := uuid + pin
rsakey, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPub, []byte(msg))
if err != nil {
return "", err
}
//fmt.Printf("the encryption is: %v", rsakey)
encodedKey := base64.StdEncoding.EncodeToString(rsakey)
fmt.Printf("the key is: %v\n", encodedKey)
fmt.Printf("The uuid is: %v\n", uuid)
return encodedKey, nil
}