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

doc: add doc for RsaSign and RsaVerifySign

This commit is contained in:
dudaodong
2024-10-11 15:38:42 +08:00
parent 527328739a
commit bad1b05224
2 changed files with 184 additions and 23 deletions

View File

@@ -70,6 +70,9 @@ import (
- [GenerateRsaKeyPair](#GenerateRsaKeyPair) - [GenerateRsaKeyPair](#GenerateRsaKeyPair)
- [RsaEncryptOAEP](#RsaEncryptOAEP) - [RsaEncryptOAEP](#RsaEncryptOAEP)
- [RsaDecryptOAEP](#RsaDecryptOAEP) - [RsaDecryptOAEP](#RsaDecryptOAEP)
- [RsaSign](#RsaSign)
- [RsaVerifySign](#RsaVerifySign)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1607,3 +1610,81 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="RsaSign">RsaSign</span>
<p>应用RSA算法签名数据。</p>
<b>函数签名:</b>
```go
func RsaSign(hash crypto.Hash, data []byte, privateKeyFileName string) ([]byte, error)
```
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
data := []byte("This is a test data for RSA signing")
hash := crypto.SHA256
privateKey := "./rsa_private.pem"
publicKey := "./rsa_public.pem"
signature, err := RsaSign(hash, data, privateKey)
if err != nil {
return
}
err = RsaVerifySign(hash, data, signature, publicKey)
if err != nil {
return
}
}
```
### <span id="RsaVerifySign">RsaVerifySign</span>
<p>验证数据的签名是否符合RSA算法。</p>
<b>函数签名:</b>
```go
func RsaVerifySign(hash crypto.Hash, data, signature []byte, pubKeyFileName string) error
```
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
data := []byte("This is a test data for RSA signing")
hash := crypto.SHA256
privateKey := "./rsa_private.pem"
publicKey := "./rsa_public.pem"
signature, err := RsaSign(hash, data, privateKey)
if err != nil {
return
}
err = RsaVerifySign(hash, data, signature, publicKey)
if err != nil {
return
}
}
```

View File

@@ -70,6 +70,8 @@ import (
- [GenerateRsaKeyPair](#GenerateRsaKeyPair) - [GenerateRsaKeyPair](#GenerateRsaKeyPair)
- [RsaEncryptOAEP](#RsaEncryptOAEP) - [RsaEncryptOAEP](#RsaEncryptOAEP)
- [RsaDecryptOAEP](#RsaDecryptOAEP) - [RsaDecryptOAEP](#RsaDecryptOAEP)
- [RsaSign](#RsaSign)
- [RsaVerifySign](#RsaVerifySign)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -1061,13 +1063,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==
} }
``` ```
@@ -1124,10 +1126,10 @@ import (
func main() { func main() {
md5Str := cryptor.Md5StringWithBase64("hello") md5Str := cryptor.Md5StringWithBase64("hello")
fmt.Println(md5Str) fmt.Println(md5Str)
// Output: // Output:
// XUFAKrxLKna5cZ2REBfFkg== // XUFAKrxLKna5cZ2REBfFkg==
} }
``` ```
@@ -1153,10 +1155,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
} }
``` ```
@@ -1182,10 +1184,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==
} }
``` ```
@@ -1268,10 +1270,10 @@ import (
func main() { func main() {
result := cryptor.Sha1WithBase64("hello") result := cryptor.Sha1WithBase64("hello")
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// qvTGHdzF6KLavt4PO0gs2a6pQ00= // qvTGHdzF6KLavt4PO0gs2a6pQ00=
} }
``` ```
@@ -1328,10 +1330,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=
} }
``` ```
@@ -1388,10 +1390,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==
} }
``` ```
@@ -1607,3 +1609,81 @@ func main() {
// hello world // hello world
} }
``` ```
### <span id="RsaSign">RsaSign</span>
<p>Signs the data with RSA algorithm.</p>
<b>Signature:</b>
```go
func RsaSign(hash crypto.Hash, data []byte, privateKeyFileName string) ([]byte, error)
```
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
data := []byte("This is a test data for RSA signing")
hash := crypto.SHA256
privateKey := "./rsa_private.pem"
publicKey := "./rsa_public.pem"
signature, err := RsaSign(hash, data, privateKey)
if err != nil {
return
}
err = RsaVerifySign(hash, data, signature, publicKey)
if err != nil {
return
}
}
```
### <span id="RsaVerifySign">RsaVerifySign</span>
<p>Verifies the signature of the data with RSA algorithm.</p>
<b>Signature:</b>
```go
func RsaVerifySign(hash crypto.Hash, data, signature []byte, pubKeyFileName string) error
```
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
data := []byte("This is a test data for RSA signing")
hash := crypto.SHA256
privateKey := "./rsa_private.pem"
publicKey := "./rsa_public.pem"
signature, err := RsaSign(hash, data, privateKey)
if err != nil {
return
}
err = RsaVerifySign(hash, data, signature, publicKey)
if err != nil {
return
}
}
```