|
- package utils
-
- import (
- "bytes"
- "crypto/rand"
- "crypto/rsa"
- "crypto/x509"
- "encoding/pem"
- "errors"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- )
-
-
- func RsaKeyGen(bits int) error {
- privateKey, err := rsa.GenerateKey(rand.Reader, bits)
- if err != nil {
- return err
- }
- derStream := x509.MarshalPKCS1PrivateKey(privateKey)
- block := &pem.Block{
- Type: "RSA PRIVATE KEY",
- Bytes: derStream,
- }
- priFile, err := os.Create("private.pem")
- if err != nil {
- return err
- }
- err = pem.Encode(priFile, block)
- priFile.Close()
- if err != nil {
- return err
- }
-
- publicKey := &privateKey.PublicKey
- derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
- if err != nil {
- return err
- }
- block = &pem.Block{
- Type: "PUBLIC KEY",
- Bytes: derPkix,
- }
- pubFile, err := os.Create("public.pem")
- if err != nil {
- return err
- }
- err = pem.Encode(pubFile, block)
- pubFile.Close()
- if err != nil {
- return err
- }
- return nil
- }
-
-
- func RsaKeyGenText(bits int) (string, string, error) {
- privateKey, err := rsa.GenerateKey(rand.Reader, bits)
- if err != nil {
- return "", "", err
- }
- derStream := x509.MarshalPKCS1PrivateKey(privateKey)
- block := &pem.Block{
- Type: "RSA PRIVATE KEY",
- Bytes: derStream,
- }
- priBuff := bytes.NewBuffer(nil)
- err = pem.Encode(priBuff, block)
- if err != nil {
- return "", "", err
- }
-
- publicKey := &privateKey.PublicKey
- derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
- if err != nil {
- return "", "", err
- }
- block = &pem.Block{
- Type: "PUBLIC KEY",
- Bytes: derPkix,
- }
- pubBuff := bytes.NewBuffer(nil)
- err = pem.Encode(pubBuff, block)
- if err != nil {
- return "", "", err
- }
- return priBuff.String(), pubBuff.String(), nil
- }
-
-
- func RsaEncrypt(rawData, publicKey []byte) ([]byte, error) {
- block, _ := pem.Decode(publicKey)
- if block == nil {
- return nil, errors.New("public key error")
- }
- pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
- if err != nil {
- return nil, err
- }
- pub := pubInterface.(*rsa.PublicKey)
- return rsa.EncryptPKCS1v15(rand.Reader, pub, rawData)
- }
-
-
- func RsaEncrypts(data, keyBytes []byte) []byte {
-
- block, _ := pem.Decode(keyBytes)
- if block == nil {
- panic(errors.New("public key error"))
- }
-
- pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
- if err != nil {
- panic(err)
- }
-
- pub := pubInterface.(*rsa.PublicKey)
-
- ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, pub, data)
- if err != nil {
- panic(err)
- }
- return ciphertext
- }
-
-
- func RsaDecrypt(cipherText, privateKey []byte) ([]byte, error) {
- block, _ := pem.Decode(privateKey)
- if block == nil {
- return nil, errors.New("private key error")
- }
- priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
- if err != nil {
- return nil, err
- }
- return rsa.DecryptPKCS1v15(rand.Reader, priv, cipherText)
- }
-
-
- func OpensslPemGetPublic(pathOrString string) (interface{}, error) {
- var certPem []byte
- var err error
- if IsFile(pathOrString) && Exists(pathOrString) {
- certPem, err = ioutil.ReadFile(pathOrString)
- if err != nil {
- return nil, err
- }
- if string(certPem) == "" {
- return nil, errors.New("empty pem file")
- }
- } else {
- if pathOrString == "" {
- return nil, errors.New("empty pem string")
- }
- certPem = StringToSlice(pathOrString)
- }
- block, rest := pem.Decode(certPem)
- if block == nil || block.Type != "PUBLIC KEY" {
-
- return nil, errors.New("failed to decode PEM block containing public key")
- }
- pub, err := x509.ParsePKIXPublicKey(block.Bytes)
- if err != nil {
- log.Fatal(err)
- }
- fmt.Printf("Got a %T, with remaining data: %q", pub, rest)
- return pub, nil
- }
|