-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package openssl | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"io" | ||
) | ||
|
||
// RSAGenerateKey generate RSA private key | ||
func RSAGenerateKey(bits int, out io.Writer) error { | ||
privateKey, err := rsa.GenerateKey(rand.Reader, bits) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) | ||
|
||
privateBlock := pem.Block{Type: "RSA PRIVATE KEY", Bytes: X509PrivateKey} | ||
|
||
return pem.Encode(out, &privateBlock) | ||
} | ||
|
||
// RSAGeneratePublicKey generate RSA public key | ||
func RSAGeneratePublicKey(priKey []byte, out io.Writer) error { | ||
block, _ := pem.Decode(priKey) | ||
// x509 parse | ||
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) | ||
if err != nil { | ||
return err | ||
} | ||
publicKey := privateKey.PublicKey | ||
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
publicBlock := pem.Block{Type: "RSA PUBLIC KEY", Bytes: X509PublicKey} | ||
|
||
return pem.Encode(out, &publicBlock) | ||
} | ||
|
||
// RSAEncrypt RSA encrypt | ||
func RSAEncrypt(src, pubKey []byte) ([]byte, error) { | ||
block, _ := pem.Decode(pubKey) | ||
// x509 parse | ||
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
publicKey, ok := publicKeyInterface.(*rsa.PublicKey) | ||
if !ok { | ||
return nil, errors.New("the kind of key is not a rsa.PublicKey") | ||
} | ||
// encrypt | ||
dst, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dst, nil | ||
} | ||
|
||
// RSADecrypt RSA decrypt | ||
func RSADecrypt(src, priKey []byte) ([]byte, error) { | ||
block, _ := pem.Decode(priKey) | ||
// x509 parse | ||
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dst, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dst, nil | ||
} | ||
|
||
// RSASign RSA sign, use crypto.SHA256 | ||
func RSASign(src []byte, priKey []byte) ([]byte, error) { | ||
block, _ := pem.Decode(priKey) | ||
// x509 parse | ||
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hash := sha256.New() | ||
_, err = hash.Write(src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes := hash.Sum(nil) | ||
sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return sign, nil | ||
} | ||
|
||
// RSAVerify RSA Verify | ||
func RSAVerify(src, sign, pubKey []byte) error { | ||
block, _ := pem.Decode(pubKey) | ||
// x509 parse | ||
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
publicKey, ok := publicKeyInterface.(*rsa.PublicKey) | ||
if !ok { | ||
return errors.New("the kind of key is not a rsa.PublicKey") | ||
} | ||
|
||
hash := sha256.New() | ||
_, err = hash.Write(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bytes := hash.Sum(nil) | ||
|
||
return rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, bytes, sign) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package openssl | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRSAEncrypt(t *testing.T) { | ||
priBuf := bytes.NewBuffer(nil) | ||
err := RSAGenerateKey(2048, priBuf) | ||
assert.NoError(t, err) | ||
t.Logf("private key: %s\n", priBuf.Bytes()) | ||
|
||
pubBuf := bytes.NewBuffer(nil) | ||
err = RSAGeneratePublicKey(priBuf.Bytes(), pubBuf) | ||
assert.NoError(t, err) | ||
t.Logf("public key: %s\n", pubBuf.Bytes()) | ||
|
||
src := []byte("123456") | ||
dst, err := RSAEncrypt(src, pubBuf.Bytes()) | ||
assert.NoError(t, err) | ||
t.Logf("encrypt out: %s\n", base64.RawStdEncoding.EncodeToString(dst)) | ||
|
||
dst, err = RSADecrypt(dst, priBuf.Bytes()) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, src, dst) | ||
|
||
t.Logf("src: %s \ndst:%s", src, dst) | ||
} | ||
|
||
func TestRSASign(t *testing.T) { | ||
priBuf := bytes.NewBuffer(nil) | ||
err := RSAGenerateKey(2048, priBuf) | ||
assert.NoError(t, err) | ||
t.Logf("private key: %s\n", priBuf.Bytes()) | ||
|
||
pubBuf := bytes.NewBuffer(nil) | ||
err = RSAGeneratePublicKey(priBuf.Bytes(), pubBuf) | ||
assert.NoError(t, err) | ||
t.Logf("public key: %s\n", pubBuf.Bytes()) | ||
|
||
src := []byte("123456") | ||
sign, err := RSASign(src, priBuf.Bytes()) | ||
assert.NoError(t, err) | ||
t.Logf("sign out: %s\n", base64.RawStdEncoding.EncodeToString(sign)) | ||
|
||
err = RSAVerify(src, sign, pubBuf.Bytes()) | ||
assert.NoError(t, err) | ||
} |