wallet/pkg/crypto/utils.go
Anton Nesterov 1382da92a3
[init]
2024-08-31 16:46:20 +02:00

33 lines
943 B
Go

package crypto
import (
"crypto/rsa"
"encoding/base64"
"math/big"
"strings"
)
func KeysToBase64(privateKey *rsa.PrivateKey) (RSAKey, RSAKey) {
pub := base64.StdEncoding.EncodeToString(privateKey.PublicKey.N.Bytes())
privBytes := privateKey.D.Bytes()[:]
privBytes = append(privBytes, '\n', '\n')
privBytes = append(privBytes, privateKey.PublicKey.N.Bytes()[:]...)
priv := base64.StdEncoding.EncodeToString(privBytes)
return RSAKey(priv), RSAKey(pub)
}
func Base64ToKeys(privateKeyBase64 string) (*rsa.PrivateKey, error) {
privateKeyStore, err := base64.StdEncoding.DecodeString(privateKeyBase64)
throw(err)
store := strings.Split(string(privateKeyStore), "\n\n")
privateKey := []byte(store[0])
publicKey := []byte(store[1])
return &rsa.PrivateKey{D: new(big.Int).SetBytes(privateKey), PublicKey: rsa.PublicKey{N: new(big.Int).SetBytes(publicKey), E: 65537}}, nil
}
func throw(err error) {
if err != nil {
panic(err)
}
}