-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
98 lines (76 loc) · 1.95 KB
/
file.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
// Package utils
package utils
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
)
// ReadFileAsBytes function read file and return bytes
func ReadFileAsBytes(filePath string) ([]byte, error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return content, nil
}
// WriteFile function write file
func WriteFile(content []byte, filename string) (err error) {
filepath := fmt.Sprintf("%s", filename)
err = ioutil.WriteFile(filepath, content, 0644)
if err != nil {
return err
}
return nil
}
// ReadPublicKeyByte function rsa read public key from file
func ReadPublicKeyByte(publicKeyData []byte) (publicKey *rsa.PublicKey, err error) {
block, _ := pem.Decode(publicKeyData)
if block == nil {
err = fmt.Errorf("failed to parse PEM block containing the key")
return
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return
}
switch pub := pub.(type) {
case *rsa.PublicKey:
publicKey = pub
default:
err = fmt.Errorf("failed to parse the file to Public Key")
}
return
}
// ReadPublicKeyFile function read file rsa public key
func ReadPublicKeyFile(filePath string) (pubKey *rsa.PublicKey, err error) {
pemByte, err := ioutil.ReadFile(filePath)
if err != nil {
return
}
pubKey, err = ReadPublicKeyByte(pemByte)
return
}
// ReadPrivateKey function RSA Read private key from byte
func ReadPrivateKey(privateKeyData []byte) (privateKey *rsa.PrivateKey, err error) {
block, _ := pem.Decode(privateKeyData)
if block == nil {
err = fmt.Errorf("failed to parse PEM block containing the key")
return
}
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return
}
return
}
// ReadPrivateKeyFile function RSA Read private key from file
func ReadPrivateKeyFile(filepath string) (privateKey *rsa.PrivateKey, err error) {
privPEM, err := ioutil.ReadFile(filepath)
if err != nil {
return
}
privateKey, err = ReadPrivateKey(privPEM)
return
}