1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 03:02:28 +08:00

doc: add doc for GenerateRsaKeyPair, RsaEncryptOAEP, RsaDecryptOAEP

This commit is contained in:
dudaodong
2023-09-14 14:15:10 +08:00
parent 8ff37f0eff
commit 17b34f8f19
2 changed files with 307 additions and 77 deletions

View File

@@ -1,6 +1,6 @@
# Cryptor # Cryptor
cryptor包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。 cryptor 包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -65,6 +65,9 @@ import (
- [GenerateRsaKey](#GenerateRsaKey) - [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt) - [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt) - [RsaDecrypt](#RsaDecrypt)
- [GenerateRsaKeyPair](#GenerateRsaKeyPair)
- [RsaEncryptOAEP](#RsaEncryptOAEP)
- [RsaDecryptOAEP](#RsaDecryptOAEP)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -988,13 +991,13 @@ import (
func main() { func main() {
str := "hello" str := "hello"
key := "12345" key := "12345"
hms := cryptor.HmacSha512WithBase64(str, key) hms := cryptor.HmacSha512WithBase64(str, key)
fmt.Println(hms) fmt.Println(hms)
// Output: // Output:
// 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A== // 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
} }
``` ```
@@ -1051,10 +1054,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5StringWithBase64("hello") md5Str := cryptor.Md5StringWithBase64("hello")
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// XUFAKrxLKna5cZ2REBfFkg== // XUFAKrxLKna5cZ2REBfFkg==
} }
``` ```
@@ -1080,10 +1083,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5Byte([]byte{'a'}) md5Str := cryptor.Md5Byte([]byte{'a'})
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// 0cc175b9c0f1b6a831c399e269772661 // 0cc175b9c0f1b6a831c399e269772661
} }
``` ```
@@ -1109,10 +1112,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5ByteWithBase64([]byte("hello")) md5Str := cryptor.Md5ByteWithBase64([]byte("hello"))
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// XUFAKrxLKna5cZ2REBfFkg== // XUFAKrxLKna5cZ2REBfFkg==
} }
``` ```
@@ -1195,10 +1198,10 @@ import (
func main() { func main() {
result := cryptor.Sha1WithBase64("hello") result := cryptor.Sha1WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// qvTGHdzF6KLavt4PO0gs2a6pQ00= // qvTGHdzF6KLavt4PO0gs2a6pQ00=
} }
``` ```
@@ -1255,10 +1258,10 @@ import (
func main() { func main() {
result := cryptor.Sha256WithBase64("hello") result := cryptor.Sha256WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ= // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
} }
``` ```
@@ -1315,10 +1318,10 @@ import (
func main() { func main() {
result := cryptor.Sha512WithBase64("hello") result := cryptor.Sha512WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw== // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
} }
``` ```
@@ -1423,3 +1426,114 @@ func main() {
// hello // hello
} }
``` ```
### <span id="GenerateRsaKeyPair">GenerateRsaKeyPair</span>
<p>创建rsa公钥私钥和key。</p>
<b>函数签名:</b>
```go
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
}
```
### <span id="RsaEncryptOAEP">RsaEncryptOAEP</span>
<p>rsa OAEP加密。</p>
<b>函数签名:</b>
```go
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/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
}
```
### <span id="RsaDecryptOAEP">RsaDecryptOAEP</span>
<p>rsa OAEP解密。</p>
<b>函数签名:</b>
```go
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
```
<b>示例:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/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
}
```

View File

@@ -1,18 +1,18 @@
# Cryptor # Cryptor
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa. Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
- [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go) - [https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go](https://github.com/duke-git/lancet/blob/main/cryptor/crypto.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/cryptor" "github.com/duke-git/lancet/v2/cryptor"
@@ -23,49 +23,48 @@ import (
## Index ## Index
- [AesEcbEncrypt](#AesEcbEncrypt) - [AesEcbEncrypt](#AesEcbEncrypt)
- [AesEcbDecrypt](#AesEcbDecrypt) - [AesEcbDecrypt](#AesEcbDecrypt)
- [AesCbcEncrypt](#AesCbcEncrypt) - [AesCbcEncrypt](#AesCbcEncrypt)
- [AesCbcDecrypt](#AesCbcDecrypt) - [AesCbcDecrypt](#AesCbcDecrypt)
- [AesCtrCrypt](#AesCtrCrypt) - [AesCtrCrypt](#AesCtrCrypt)
- [AesCfbEncrypt](#AesCfbEncrypt) - [AesCfbEncrypt](#AesCfbEncrypt)
- [AesCfbDecrypt](#AesCfbDecrypt) - [AesCfbDecrypt](#AesCfbDecrypt)
- [AesOfbEncrypt](#AesOfbEncrypt) - [AesOfbEncrypt](#AesOfbEncrypt)
- [AesOfbDecrypt](#AesOfbDecrypt) - [AesOfbDecrypt](#AesOfbDecrypt)
- [Base64StdEncode](#Base64StdEncode) - [Base64StdEncode](#Base64StdEncode)
- [Base64StdDecode](#Base64StdDecode) - [Base64StdDecode](#Base64StdDecode)
- [DesEcbEncrypt](#DesEcbEncrypt) - [DesEcbEncrypt](#DesEcbEncrypt)
- [DesEcbDecrypt](#DesEcbDecrypt) - [DesEcbDecrypt](#DesEcbDecrypt)
- [DesCbcEncrypt](#DesCbcEncrypt) - [DesCbcEncrypt](#DesCbcEncrypt)
- [DesCbcDecrypt](#DesCbcDecrypt) - [DesCbcDecrypt](#DesCbcDecrypt)
- [DesCtrCrypt](#DesCtrCrypt) - [DesCtrCrypt](#DesCtrCrypt)
- [DesCfbEncrypt](#DesCfbEncrypt) - [DesCfbEncrypt](#DesCfbEncrypt)
- [DesCfbDecrypt](#DesCfbDecrypt) - [DesCfbDecrypt](#DesCfbDecrypt)
- [DesOfbEncrypt](#DesOfbEncrypt) - [DesOfbEncrypt](#DesOfbEncrypt)
- [DesOfbDecrypt](#DesOfbDecrypt) - [DesOfbDecrypt](#DesOfbDecrypt)
- [HmacMd5](#HmacMd5) - [HmacMd5](#HmacMd5)
- [HmacMd5WithBase64](#HmacMd5WithBase64) - [HmacMd5WithBase64](#HmacMd5WithBase64)
- [HmacSha1](#HmacSha1) - [HmacSha1](#HmacSha1)
- [HmacSha1WithBase64](#HmacSha1WithBase64) - [HmacSha1WithBase64](#HmacSha1WithBase64)
- [HmacSha256](#HmacSha256) - [HmacSha256](#HmacSha256)
- [HmacSha256WithBase64](#HmacSha256WithBase64) - [HmacSha256WithBase64](#HmacSha256WithBase64)
- [HmacSha512](#HmacSha512) - [HmacSha512](#HmacSha512)
- [HmacSha512WithBase64](#HmacSha512WithBase64) - [HmacSha512WithBase64](#HmacSha512WithBase64)
- [Md5String](#Md5String) - [Md5String](#Md5String)
- [Md5StringWithBase64](#Md5StringWithBase64) - [Md5StringWithBase64](#Md5StringWithBase64)
- [Md5Byte](#Md5Byte) - [Md5Byte](#Md5Byte)
- [Md5ByteWithBase64](#Md5ByteWithBase64) - [Md5ByteWithBase64](#Md5ByteWithBase64)
- [Md5File](#Md5File) - [Md5File](#Md5File)
- [Sha1](#Sha1) - [Sha1](#Sha1)
- [Sha1WithBase64](#Sha1WithBase64) - [Sha1WithBase64](#Sha1WithBase64)
- [Sha256](#Sha256) - [Sha256](#Sha256)
- [Sha256WithBase64](#Sha256WithBase64) - [Sha256WithBase64](#Sha256WithBase64)
- [Sha512](#Sha512) - [Sha512](#Sha512)
- [Sha512WithBase64](#Sha512WithBase64) - [Sha512WithBase64](#Sha512WithBase64)
- [GenerateRsaKey](#GenerateRsaKey) - [GenerateRsaKey](#GenerateRsaKey)
- [RsaEncrypt](#RsaEncrypt) - [RsaEncrypt](#RsaEncrypt)
- [RsaDecrypt](#RsaDecrypt) - [RsaDecrypt](#RsaDecrypt)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -80,6 +79,7 @@ import (
```go ```go
func AesEcbEncrypt(data, key []byte) []byte func AesEcbEncrypt(data, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
@@ -113,6 +113,7 @@ func main() {
```go ```go
func AesEcbDecrypt(encrypted, key []byte) []byte func AesEcbDecrypt(encrypted, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zI6xsmuQRbn)</span></b>
```go ```go
@@ -146,6 +147,7 @@ func main() {
```go ```go
func AesCbcEncrypt(data, key []byte) []byte func AesCbcEncrypt(data, key []byte) []byte
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/IOq_g8_lKZD)</span></b>
```go ```go
@@ -339,6 +341,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="AesCfbDecrypt">AesOfbDecrypt</span> ### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p> <p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
@@ -382,6 +385,7 @@ func main() {
```go ```go
func Base64StdEncode(s string) string func Base64StdEncode(s string) string
``` ```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/VOaUyQUreoK)</span></b> <b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/VOaUyQUreoK)</span></b>
```go ```go
@@ -400,6 +404,7 @@ func main() {
// aGVsbG8= // aGVsbG8=
} }
``` ```
### <span id="Base64StdDecode">Base64StdDecode</span> ### <span id="Base64StdDecode">Base64StdDecode</span>
<p>Decode a base64 encoded string.</p> <p>Decode a base64 encoded string.</p>
@@ -463,6 +468,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesEcbDecrypt">DesEcbDecrypt</span> ### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
@@ -565,6 +571,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCtrCrypt">DesCtrCrypt</span> ### <span id="DesCtrCrypt">DesCtrCrypt</span>
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p> <p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
@@ -632,6 +639,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesCfbDecrypt">DesCfbDecrypt</span> ### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
@@ -665,6 +673,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbEncrypt">DesOfbEncrypt</span> ### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -698,6 +707,7 @@ func main() {
// hello // hello
} }
``` ```
### <span id="DesOfbDecrypt">DesOfbDecrypt</span> ### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p> <p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
@@ -988,7 +998,6 @@ func main() {
} }
``` ```
### <span id="Md5String">Md5String</span> ### <span id="Md5String">Md5String</span>
<p>Get the md5 value of string.</p> <p>Get the md5 value of string.</p>
@@ -1378,7 +1387,6 @@ func main() {
} }
``` ```
### <span id="RsaDecrypt">RsaDecrypt</span> ### <span id="RsaDecrypt">RsaDecrypt</span>
<p>Decrypt data with private key file useing ras algorithm.</p> <p>Decrypt data with private key file useing ras algorithm.</p>
@@ -1416,5 +1424,113 @@ func main() {
} }
``` ```
### <span id="GenerateRsaKeyPair">GenerateRsaKeyPair</span>
<p>Creates rsa private and public key.</p>
<b>Signature:</b>
```go
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
pri, pub := cryptor.GenerateRsaKeyPair(1024)
}
```
### <span id="RsaEncryptOAEP">RsaEncryptOAEP</span>
<p>Encrypts the given data with RSA-OAEP.</p>
<b>Signature:</b>
```go
func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/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
}
```
### <span id="RsaDecryptOAEP">RsaDecryptOAEP</span>
<p>Decrypts the data with RSA-OAEP.</p>
<b>Signature:</b>
```go
func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)
```
<b>Example:></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/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
}
```