wallet/pkg/crypto/utils.go

33 lines
943 B
Go
Raw Permalink Normal View History

2024-08-31 14:46:20 +00:00
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)
}
}