Version 0.0.1

This commit is contained in:
Naoki Kosaka
2018-11-07 01:44:50 +09:00
parent e5d9a647bd
commit 033e35dd6d
13 changed files with 1898 additions and 0 deletions

46
KeyLoader/keyloader.go Normal file
View File

@ -0,0 +1,46 @@
package keyloader
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"unsafe"
)
func ReadPrivateKeyRSAfromPath(path string) (*rsa.PrivateKey, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
decoded, _ := pem.Decode(file)
priv, err := x509.ParsePKCS1PrivateKey(decoded.Bytes)
if err != nil {
return nil, err
}
return priv, nil
}
func ReadPublicKeyRSAfromString(pemString string) (*rsa.PublicKey, error) {
pemByte := *(*[]byte)(unsafe.Pointer(&pemString))
decoded, _ := pem.Decode(pemByte)
keyInterface, err := x509.ParsePKIXPublicKey(decoded.Bytes)
if err != nil {
fmt.Println(err)
return nil, err
}
pub := keyInterface.(*rsa.PublicKey)
return pub, nil
}
func GeneratePublicKeyPEMString(pub *rsa.PublicKey) string {
pubkeyBytes := x509.MarshalPKCS1PublicKey(pub)
pubkeyPem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubkeyBytes,
},
)
return string(pubkeyPem)
}