Skip to content

Cryptor

cryptor 包包含数据加密和解密功能。支持 base64, md5, hmac, hash, aes, des, rsa。

源码:

用法:

go
import (
    "github.com/duke-git/lancet/v2/cryptor"
)

目录

文档

AesEcbEncrypt

使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesEcbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesEcbDecrypt

使用AES ECB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesEcbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesCbcEncrypt

使用AES CBC算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesCbcEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesCbcDecrypt

使用AES CBC算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesCbcDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesCtrCrypt

使用AES CTR算法模式加密/解密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesCtrCrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesCfbEncrypt

使用AES CFB算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesCfbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesCfbDecrypt

使用AES CFB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesCfbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesOfbEncrypt

使用AES OFB算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesOfbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesOfbDecrypt

使用AES OFB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名:

go
func AesOfbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesGcmEncrypt

使用AES GCM算法模式加密数据。

函数签名:

go
func AesGcmEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

AesGcmDecrypt

使用AES GCM算法解密数据。

函数签名:

go
func AesGcmDecrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefghijklmnop"

    encrypted := cryptor.AesGcmEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesGcmDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

Base64StdEncode

将字符串base64编码。

函数签名:

go
func Base64StdEncode(s string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    base64Str := cryptor.Base64StdEncode("hello")
    fmt.Println(base64Str)

    // Output:
    // aGVsbG8=
}

Base64StdDecode

解码base64字符串。

函数签名:

go
func Base64StdDecode(s string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := cryptor.Base64StdDecode("aGVsbG8=")
    fmt.Println(str)

    // Output:
    // hello
}

DesEcbEncrypt

使用DES ECB算法模式加密数据,参数`key`的长度是8。

函数签名:

go
func DesEcbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))

    decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesEcbDecrypt

使用DES ECB算法模式解决密数据,参数`key`的长度是8。

函数签名:

go
func DesEcbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))

    decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesCbcEncrypt

使用DES CBC算法模式加密数据,参数`key`的长度是8。

函数签名:

go
func DesCbcEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesCbcDecrypt

使用DES CBC算法模式解密数据,参数`key`的长度是8。

函数签名:

go
func DesCbcDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesCtrCrypt

使用DES CTR算法模式加密/解密数据,参数`key`的长度是8

函数签名:

go
func DesCtrCrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesCfbEncrypt

使用DES CFB算法模式加密数据,参数`key`的长度是8。

函数签名:

go
func DesCfbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesCfbDecrypt

使用DES CFB算法模式解决密数据,参数`key`的长度是8。

函数签名:

go
func DesCfbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesOfbEncrypt

使用DES OFB算法模式加密数据,参数`key`的长度是8。

函数签名:

go
func DesOfbEncrypt(data, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

DesOfbDecrypt

使用DES OFB算法模式解密数据,参数`key`的长度是8。

函数签名:

go
func DesOfbDecrypt(encrypted, key []byte) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    data := "hello"
    key := "abcdefgh"

    encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

HmacMd5

获取字符串md5 hmac值。

函数签名:

go
func HmacMd5(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacMd5(str, key)
    fmt.Println(hms)

    // Output:
    // e834306eab892d872525d4918a7a639a
}

HmacMd5WithBase64

获取字符串md5 hmac base64字符串值。

函数签名:

go
func HmacMd5WithBase64(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacMd5WithBase64(str, key)
    fmt.Println(hms)

    // Output:
    // 6DQwbquJLYclJdSRinpjmg==
}

HmacSha1

获取字符串的sha1 hmac值。

函数签名:

go
func HmacSha1(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha1(str, key)
    fmt.Println(hms)

    // Output:
    // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
}

HmacSha1WithBase64

获取字符串的sha1 base64值。

函数签名:

go
func HmacSha1WithBase64(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha1WithBase64(str, key)
    fmt.Println(hms)

    // Output:
    // XGqdsMzLkuNu0DI/0Jt/k23prOA=
}

HmacSha256

获取字符串sha256 hmac值。

函数签名:

go
func HmacSha256(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha256(str, key)
    fmt.Println(hms)

    // Output:
    // 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
}

HmacSha256WithBase64

获取字符串sha256 hmac base64值。

函数签名:

go
func HmacSha256WithBase64(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha256WithBase64(str, key)
    fmt.Println(hms)

    // Output:
    // MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
}

HmacSha512

获取字符串sha512 hmac值。

函数签名:

go
func HmacSha512(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha512(str, key)
    fmt.Println(hms)

    // Output:
    // dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
}

HmacSha512WithBase64

获取字符串sha512 hmac base64值。

函数签名:

go
func HmacSha512WithBase64(str, key string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"
    key := "12345"

    hms := cryptor.HmacSha512WithBase64(str, key)
    fmt.Println(hms)

    // Output:
    // 3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==
}

Md5String

获取字符串md5值。

函数签名:

go
func Md5String(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"

    md5Str := cryptor.Md5String(str)
    fmt.Println(md5Str)

    // Output:
    // 5d41402abc4b2a76b9719d911017c592
}

Md5StringWithBase64

获取字符串md5 base64值。

函数签名:

go
func Md5StringWithBase64(s string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    md5Str := cryptor.Md5StringWithBase64("hello")
    fmt.Println(md5Str)

    // Output:
    // XUFAKrxLKna5cZ2REBfFkg==
}

Md5Byte

获取byte slice的md5值。

函数签名:

go
func Md5Byte(data []byte) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    md5Str := cryptor.Md5Byte([]byte{'a'})
    fmt.Println(md5Str)

    // Output:
    // 0cc175b9c0f1b6a831c399e269772661
}

Md5ByteWithBase64

获取byte slice的md5 base64值。

函数签名:

go
func Md5ByteWithBase64(data []byte) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    md5Str := cryptor.Md5ByteWithBase64([]byte("hello"))
    fmt.Println(md5Str)

    // Output:
    // XUFAKrxLKna5cZ2REBfFkg==
}

Md5File

获取文件md5值。

函数签名:

go
func Md5File(filepath string) (string, error)

示例:

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    s := cryptor.Md5File("./main.go"))
    fmt.Println(s)
}

Sha1

获取字符串sha1值。

函数签名:

go
func Sha1(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"

    result := cryptor.Sha1(str)
    fmt.Println(result)

    // Output:
    // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
}

Sha1WithBase64

获取字符串sha1 base64值。

函数签名:

go
func Sha1WithBase64(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    result := cryptor.Sha1WithBase64("hello")
    fmt.Println(result)

    // Output:
    // qvTGHdzF6KLavt4PO0gs2a6pQ00=
}

Sha256

获取字符串sha256值。

函数签名:

go
func Sha256(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"

    result := cryptor.Sha256(str)
    fmt.Println(result)

    // Output:
    // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}

Sha256WithBase64

获取字符串sha256 base64值。

函数签名:

go
func Sha256WithBase64(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    result := cryptor.Sha256WithBase64("hello")
    fmt.Println(result)

    // Output:
    // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
}

Sha512

获取字符串sha512值。

函数签名:

go
func Sha512(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    str := "hello"

    result := cryptor.Sha512(str)
    fmt.Println(result)

    // Output:
    // 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
}

Sha512WithBase64

获取字符串sha512 base64值。

函数签名:

go
func Sha512WithBase64(str string) string

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    result := cryptor.Sha512WithBase64("hello")
    fmt.Println(result)

    // Output:
    // m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==
}

GenerateRsaKey

在当前目录下创建rsa私钥文件和公钥文件。

函数签名:

go
func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
    if err != nil {
        fmt.Println(err)
    }
}

RsaEncrypt

用公钥文件ras加密数据。

函数签名:

go
func RsaEncrypt(data []byte, pubKeyFileName string) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
    if err != nil {
        return
    }

    data := []byte("hello")
    encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
    decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

RsaDecrypt

用私钥文件rsa解密数据。

函数签名:

go
func RsaDecrypt(data []byte, privateKeyFileName string) []byte

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/cryptor"
)

func main() {
    err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
    if err != nil {
        return
    }

    data := []byte("hello")
    encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
    decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")

    fmt.Println(string(decrypted))

    // Output:
    // hello
}

GenerateRsaKeyPair

创建rsa公钥私钥和key。

函数签名:

go
func GenerateRsaKeyPair(keySize int) (*rsa.PrivateKey, *rsa.PublicKey)

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/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/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
}

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/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
}

RsaSign

应用RSA算法签名数据。

函数签名:

go
func RsaSign(hash crypto.Hash, data []byte, privateKeyFileName string) ([]byte, error)

示例:运行

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 := cryptor.RsaSign(hash, data, privateKey)
    if err != nil {
        return
    }

    err = cryptor.RsaVerifySign(hash, data, signature, publicKey)
    if err != nil {
        return
    }
}

RsaVerifySign

验证数据的签名是否符合RSA算法。

函数签名:

go
func RsaVerifySign(hash crypto.Hash, data, signature []byte, pubKeyFileName string) error

示例:运行

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 := cryptor.RsaSign(hash, data, privateKey)
    if err != nil {
        return
    }

    err = cryptor.RsaVerifySign(hash, data, signature, publicKey)
    if err != nil {
        return
    }
}