diff --git a/cryptor/crypto.go b/cryptor/crypto.go index 52da6f8..3abe100 100644 --- a/cryptor/crypto.go +++ b/cryptor/crypto.go @@ -13,6 +13,7 @@ import ( "crypto/des" "crypto/rand" "crypto/rsa" + "crypto/sha256" "crypto/x509" "encoding/pem" "io" @@ -461,3 +462,29 @@ func RsaDecrypt(data []byte, privateKeyFileName string) []byte { } return plainText } + +// GenerateRsaKeyPair create rsa private and public key. +func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) { + privateKey, _ := rsa.GenerateKey(rand.Reader, keySize) + return privateKey, &privateKey.PublicKey +} + +// RsaEncryptOAEP encrypts the given data with RSA-OAEP. +func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) { + encryptedBytes, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, &key, data, label) + if err != nil { + return nil, err + } + + return encryptedBytes, nil +} + +// RsaDecryptOAEP decrypts the data with RSA-OAEP. +func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) { + decryptedBytes, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, &key, ciphertext, label) + if err != nil { + return nil, err + } + + return decryptedBytes, nil +} diff --git a/cryptor/crypto_test.go b/cryptor/crypto_test.go index ade588e..fda5663 100644 --- a/cryptor/crypto_test.go +++ b/cryptor/crypto_test.go @@ -128,3 +128,21 @@ func TestRsaEncrypt(t *testing.T) { assert := internal.NewAssert(t, "TestRsaEncrypt") assert.Equal(string(data), string(decrypted)) } + +func TestRsaEncryptOAEP(t *testing.T) { + assert := internal.NewAssert(t, "TestRsaEncrypt") + t.Parallel() + + pri, pub := GenerateRsaKeyPair(1024) + + data := []byte("hello world") + label := []byte("123456") + + encrypted, err := RsaEncryptOAEP(data, label, *pub) + assert.IsNil(err) + + decrypted, err := RsaDecryptOAEP([]byte(encrypted), label, *pri) + + assert.IsNil(err) + assert.Equal("hello world", string(decrypted)) +} diff --git a/docs/cryptor.md b/docs/cryptor.md index bb6d36b..04e68e3 100644 --- a/docs/cryptor.md +++ b/docs/cryptor.md @@ -65,6 +65,9 @@ import ( - [GenerateRsaKey](#GenerateRsaKey) - [RsaEncrypt](#RsaEncrypt) - [RsaDecrypt](#RsaDecrypt) +- [GenerateRsaKeyPair](#GenerateRsaKeyPair) +- [RsaEncryptOAEP](#RsaEncryptOAEP) +- [RsaDecryptOAEP](#RsaDecryptOAEP)
@@ -723,6 +726,7 @@ func main() { fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd } ``` + ### HmacSha1WithBase64Return the hmac hash of string use sha1 with base64.
@@ -1270,3 +1274,114 @@ func main() { fmt.Println(string(decrypted)) //hello world } ``` + +### GenerateRsaKeyPair + +Creates rsa private and public key.
+ +Signature: + +```go +func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) +``` + +Example:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) +} +``` + +### RsaEncryptOAEP + +Encrypts the given data with RSA-OAEP.
+ +Signature: + +```go +func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) +``` + +Example:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) + + data := []byte("hello world") + label := []byte("123456") + + encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) + if err != nil { + return + } + + decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) + if err != nil { + return + } + + fmt.Println(string(decrypted)) + + // Output: + // hello world +} +``` + +### RsaDecryptOAEP + +Decrypts the data with RSA-OAEP.
+ +Signature: + +```go +func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) +``` + +Example:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) + + data := []byte("hello world") + label := []byte("123456") + + encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) + if err != nil { + return + } + + decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) + if err != nil { + return + } + + fmt.Println(string(decrypted)) + + // Output: + // hello world +} +``` diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index 1a9e694..d912b64 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -64,6 +64,9 @@ import ( - [GenerateRsaKey](#GenerateRsaKey) - [RsaEncrypt](#RsaEncrypt) - [RsaDecrypt](#RsaDecrypt) +- [GenerateRsaKeyPair](#GenerateRsaKeyPair) +- [RsaEncryptOAEP](#RsaEncryptOAEP) +- [RsaDecryptOAEP](#RsaDecryptOAEP) @@ -754,7 +757,6 @@ func main() { } ``` - ### HmacSha256获取字符串sha256 hmac值。
@@ -883,7 +885,6 @@ func main() { } ``` - ### Md5String获取字符串md5值。
@@ -1301,3 +1302,114 @@ func main() { fmt.Println(string(decrypted)) //hello world } ``` + +### GenerateRsaKeyPair + +创建rsa公钥私钥和key。
+ +函数签名: + +```go +func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey) +``` + +示例:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) +} +``` + +### RsaEncryptOAEP + +rsa OAEP加密。
+ +函数签名: + +```go +func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error) +``` + +示例:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) + + data := []byte("hello world") + label := []byte("123456") + + encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) + if err != nil { + return + } + + decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) + if err != nil { + return + } + + fmt.Println(string(decrypted)) + + // Output: + // hello world +} +``` + +### RsaDecryptOAEP + +rsa OAEP解密。
+ +函数签名: + +```go +func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error) +``` + +示例:> + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/cryptor" +) + +func main() { + pri, pub := cryptor.GenerateRsaKeyPair(1024) + + data := []byte("hello world") + label := []byte("123456") + + encrypted, err := cryptor.RsaEncryptOAEP(data, label, *pub) + if err != nil { + return + } + + decrypted, err := cryptor.RsaDecryptOAEP([]byte(encrypted), label, *pri) + if err != nil { + return + } + + fmt.Println(string(decrypted)) + + // Output: + // hello world +} +```