1
0
mirror of https://github.com/fumiama/tienyik.git synced 2026-06-04 23:10:26 +08:00
Files
tienyik/rsa.go
2025-10-30 23:23:45 +08:00

33 lines
615 B
Go

package tienyik
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
)
type RSA rsa.PrivateKey
func NewRSA() *RSA {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
privateKey.E = 0x010001
return (*RSA)(privateKey)
}
func (tyr *RSA) PublicKeyToSPKI() string {
spkiBytes, err := x509.MarshalPKIXPublicKey(&tyr.PublicKey)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(spkiBytes)
}
func (tyr *RSA) Decrypt(ciphertext []byte) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, (*rsa.PrivateKey)(tyr), ciphertext)
}