1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/cryptor.md
2023-09-17 16:21:40 +08:00

24 KiB

Cryptor

Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.

Source:

Usage:

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

Index

Documentation

AesEcbEncrypt

Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
      key := "abcdefghijklmnop"
    encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))

    fmt.Println(string(encrypted))
}

AesEcbDecrypt

Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
    fmt.Println(string(decrypted)) //hello world
}

AesCbcEncrypt

Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))

    fmt.Println(string(encrypted))
}

AesCbcDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
    fmt.Println(string(decrypted)) //hello world
}

AesCtrCrypt

Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

AesCfbEncrypt

Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
    fmt.Println(string(encrypted))
}

AesCfbDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
    fmt.Println(string(decrypted)) //hello world
}

AesOfbEncrypt

Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
    fmt.Println(string(encrypted))
}

AesOfbDecrypt

Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefghijklmnop"
    encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

Base64StdEncode

Encode string with base64 encoding.

Signature:

func Base64StdEncode(s string) string

Example:

package main

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

func main() {
    base64Str := cryptor.Base64StdEncode("hello world")
    fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
}

Base64StdDecode

Decode a base64 encoded string.

Signature:

func Base64StdDecode(s string) string

Example:

package main

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

func main() {
    str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
    fmt.Println(str) //hello world
}

DesEcbEncrypt

Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))

    fmt.Println(string(encrypted))
}

DesEcbDecrypt

Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
    decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

DesCbcEncrypt

Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)

    fmt.Println(string(encrypted))
}

DesCbcDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
    decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

DesCtrCrypt

Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

DesCfbEncrypt

Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
    fmt.Println(string(encrypted))
}

DesCfbDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
    decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
    fmt.Println(string(decrypted)) //hello world
}

DesOfbEncrypt

Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
    fmt.Println(string(encrypted))
}

DesOfbDecrypt

Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

Signature:

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

Example:

package main

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

func main() {
    data := "hello world"
    key := "abcdefgh"
    encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
    decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))

    fmt.Println(string(decrypted)) //hello world
}

HmacMd5

Get the md5 hmac hash of string.

Signature:

func HmacMd5(data, key string) string

Example:

package main

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

func main() {
    s := cryptor.HmacMd5("hello world", "12345"))
    fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
}

HmacMd5WithBase64

Return the hmac hash of string use md5 with base64.

Signature:

func HmacMd5WithBase64(data, key string) string

Example:

package main

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

func main() {
    s := cryptor.HmacMd5WithBase64("hello", "12345"))
    fmt.Println(s) //6DQwbquJLYclJdSRinpjmg==
}

HmacSha1

Get the sha1 hmac hash of string.

Signature:

func HmacSha1(data, key string) string

Example:

package main

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

func main() {
    s := cryptor.HmacSha1("hello world", "12345"))
    fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
}

HmacSha1WithBase64

Return the hmac hash of string use sha1 with base64.

Signature:

func HmacSha1WithBase64(str, key string) string

Example:

package main

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

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

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

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

HmacSha256

Get the sha256 hmac hash of string

Signature:

func HmacSha256(data, key string) string

Example:

package main

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

func main() {
    s := cryptor.HmacSha256("hello world", "12345"))
    fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
}

HmacSha256WithBase64

Return the hmac hash of string use sha256 with base64.

Signature:

func HmacSha256WithBase64(str, key string) string

Example:

package main

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

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

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

    // Output:
    // MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=
}

HmacSha512

Get the sha512 hmac hash of string.

Signature:

func HmacSha512(data, key string) string

Example:

package main

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

func main() {
    s := cryptor.HmacSha512("hello world", "12345"))
    fmt.Println(s) //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
}

HmacSha512WithBase64

Return the hmac hash of string use sha512 with base64.

Signature:

func HmacSha512WithBase64(str, key string) string

Example:

package main

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

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

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

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

Md5String

Get the md5 value of string.

Signature:

func Md5String(s string) string

Example:

package main

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

func main() {
    s := cryptor.Md5String("hello"))
    fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
}

Md5StringWithBase64

Return the md5 value of string with base64.

Signature:

func Md5StringWithBase64(s string) string

Example:

package main

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

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

    // Output:
    // XUFAKrxLKna5cZ2REBfFkg==
}

Md5Byte

Return the md5 string of byte slice.

Signature:

func Md5Byte(data []byte) string

Example:

package main

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

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

    // Output:
    // 0cc175b9c0f1b6a831c399e269772661
}

Md5ByteWithBase64

Return the md5 string of byte slice with base64.

Signature:

func Md5ByteWithBase64(data []byte) string

Example:

package main

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

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

    // Output:
    // XUFAKrxLKna5cZ2REBfFkg==
}

Md5File

Get the md5 value of file.

Signature:

func Md5File(filepath string) (string, error)

Example:

package main

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

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

Sha1

Get the sha1 value of string.

Signature:

func Sha1(data string) string

Example:

package main

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

func main() {
    s := cryptor.Sha1("hello world"))
    fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
}

Sha1WithBase64

Return the sha1 value (SHA-1 hash algorithm) of base64 string.

Signature:

func Sha1WithBase64(str string) string

Example:

package main

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

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

    // Output:
    // qvTGHdzF6KLavt4PO0gs2a6pQ00=
}

Sha256

Get the sha256 value of string.

Signature:

func Sha256(data string) string

Example:

package main

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

func main() {
    s := cryptor.Sha256("hello world"))
    fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
}

Sha256WithBase64

Return the sha256 value (SHA256 hash algorithm) of base64 string.

Signature:

func Sha256WithBase64(str string) string

Example:

package main

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

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

    // Output:
    // LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=
}

Sha512

Get the sha512 value of string.

Signature:

func Sha512(data string) string

Example:

package main

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

func main() {
    s := cryptor.Sha512("hello world"))
    fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
}

Sha512WithBase64

Get the sha512 value of string with base64.

Signature:

func Sha512WithBase64(str string) string

Example:

package main

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

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

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

GenerateRsaKey

Create the rsa public and private key file in current directory.

Signature:

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

Example:

package main

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

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

RsaEncrypt

Encrypt data with public key file useing ras algorithm.

Signature:

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

Example:

package main

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

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

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

    fmt.Println(string(decrypted)) //hello world
}

RsaDecrypt

Decrypt data with private key file useing ras algorithm.

Signature:

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

Example:

package main

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

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

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

    fmt.Println(string(decrypted)) //hello world
}

GenerateRsaKeyPair

Creates rsa private and public key.

Signature:

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

Example:>

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:

func RsaEncryptOAEP(data []byte, label []byte, key rsa.PublicKey) ([]byte, error)

Example:>

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:

func RsaDecryptOAEP(ciphertext []byte, label []byte, key rsa.PrivateKey) ([]byte, error)

Example:>

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
}