cryptor

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 19 Imported by: 28

Documentation

Overview

Package cryptor implements some util functions to encrypt and decrypt. Contain base64, hmac, sha, aes, des, and rsa

Package cryptor implements some util functions to encrypt and decrypt. Note: 1. for aes crypt function, the `key` param length should be 16, 24 or 32. if not, will panic.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AesCbcDecrypt

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

AesCbcDecrypt decrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/IOq_g8_lKZD

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesCbcEncrypt([]byte(data), []byte(key))

decrypted := AesCbcDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesCbcEncrypt

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

AesCbcEncrypt encrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/IOq_g8_lKZD

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesCbcEncrypt([]byte(data), []byte(key))

decrypted := AesCbcDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesCfbDecrypt

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

AesCfbDecrypt decrypt data with key use AES CFB algorithm len(encrypted) should be great than 16, len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/tfkF10B13kH

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesCfbEncrypt([]byte(data), []byte(key))

decrypted := AesCfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesCfbEncrypt

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

AesCfbEncrypt encrypt data with key use AES CFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/tfkF10B13kH

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesCfbEncrypt([]byte(data), []byte(key))

decrypted := AesCfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesCtrCrypt

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

AesCtrCrypt encrypt data with key use AES CTR algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/SpaZO0-5Nsp

Example
data := "hello"
key := "abcdefghijklmnop"

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

fmt.Println(string(decrypted))
Output:

hello

func AesEcbDecrypt

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

AesEcbDecrypt decrypt data with key use AES ECB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/jT5irszHx-j

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesEcbEncrypt([]byte(data), []byte(key))

decrypted := AesEcbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesEcbEncrypt

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

AesEcbEncrypt encrypt data with key use AES ECB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/jT5irszHx-j

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesEcbEncrypt([]byte(data), []byte(key))

decrypted := AesEcbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesOfbDecrypt

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

AesOfbDecrypt decrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/VtHxtkUj-3F

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesOfbEncrypt([]byte(data), []byte(key))

decrypted := AesOfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func AesOfbEncrypt

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

AesOfbEncrypt encrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32. Play: https://go.dev/play/p/VtHxtkUj-3F

Example
data := "hello"
key := "abcdefghijklmnop"

encrypted := AesOfbEncrypt([]byte(data), []byte(key))

decrypted := AesOfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func Base64StdDecode

func Base64StdDecode(s string) string

Base64StdDecode decode a base64 encoded string. Play: https://go.dev/play/p/RWQylnJVgIe

Example
str := Base64StdDecode("aGVsbG8=")

fmt.Println(str)
Output:

hello

func Base64StdEncode

func Base64StdEncode(s string) string

Base64StdEncode encode string with base64 encoding. Play: https://go.dev/play/p/VOaUyQUreoK

Example
base64Str := Base64StdEncode("hello")

fmt.Println(base64Str)
Output:

aGVsbG8=

func DesCbcDecrypt

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

DesCbcDecrypt decrypt data with key use DES CBC algorithm len(key) should be 8. Play: https://go.dev/play/p/4cC4QvWfe3_1

Example
data := "hello"
key := "abcdefgh"

encrypted := DesCbcEncrypt([]byte(data), []byte(key))

decrypted := DesCbcDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func DesCbcEncrypt

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

DesCbcEncrypt encrypt data with key use DES CBC algorithm len(key) should be 8. Play: https://go.dev/play/p/4cC4QvWfe3_1

Example
data := "hello"
key := "abcdefgh"

encrypted := DesCbcEncrypt([]byte(data), []byte(key))

decrypted := DesCbcDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func DesCfbDecrypt

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

DesCfbDecrypt decrypt data with key use DES CFB algorithm len(encrypted) should be great than 16, len(key) should be 8. Play: https://go.dev/play/p/y-eNxcFBlxL

Example
data := "hello"
key := "abcdefgh"

encrypted := DesCfbEncrypt([]byte(data), []byte(key))

decrypted := DesCfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func DesCfbEncrypt

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

DesCfbEncrypt encrypt data with key use DES CFB algorithm len(key) should be 8. Play: https://go.dev/play/p/y-eNxcFBlxL

Example
data := "hello"
key := "abcdefgh"

encrypted := DesCfbEncrypt([]byte(data), []byte(key))

decrypted := DesCfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func DesCtrCrypt

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

DesCtrCrypt encrypt data with key use DES CTR algorithm len(key) should be 8. Play: https://go.dev/play/p/9-T6OjKpcdw

Example
data := "hello"
key := "abcdefgh"

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

fmt.Println(string(decrypted))
Output:

hello

func DesEcbDecrypt

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

DesEcbDecrypt decrypt data with key use DES ECB algorithm len(key) should be 8. Play: https://go.dev/play/p/8qivmPeZy4P

Example
data := "hello"
key := "abcdefgh"

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

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

fmt.Println(string(decrypted))
Output:

hello

func DesEcbEncrypt

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

DesEcbEncrypt encrypt data with key use DES ECB algorithm len(key) should be 8. Play: https://go.dev/play/p/8qivmPeZy4P

Example
data := "hello"
key := "abcdefgh"

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

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

fmt.Println(string(decrypted))
Output:

hello

func DesOfbDecrypt

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

DesOfbDecrypt decrypt data with key use DES OFB algorithm len(key) should be 8. Play: https://go.dev/play/p/74KmNadjN1J

Example
data := "hello"
key := "abcdefgh"

encrypted := DesOfbEncrypt([]byte(data), []byte(key))

decrypted := DesOfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func DesOfbEncrypt

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

DesOfbEncrypt encrypt data with key use DES OFB algorithm len(key) should be 8. Play: https://go.dev/play/p/74KmNadjN1J

Example
data := "hello"
key := "abcdefgh"

encrypted := DesOfbEncrypt([]byte(data), []byte(key))

decrypted := DesOfbDecrypt(encrypted, []byte(key))

fmt.Println(string(decrypted))
Output:

hello

func GenerateRsaKey

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

GenerateRsaKey create rsa private and public pemo file. Play: https://go.dev/play/p/zutRHrDqs0X

Example
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
	return
}

fmt.Println("foo")
Output:

foo

func GenerateRsaKeyPair added in v2.2.7

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

GenerateRsaKeyPair create rsa private and public key. Play: https://go.dev/play/p/sSVmkfENKMz

func HmacMd5

func HmacMd5(str, key string) string

HmacMd5 return the hmac hash of string use md5. Play: https://go.dev/play/p/uef0q1fz53I

Example
str := "hello"
key := "12345"

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

e834306eab892d872525d4918a7a639a

func HmacMd5WithBase64 added in v2.2.4

func HmacMd5WithBase64(data, key string) string

HmacMd5WithBase64 return the hmac hash of string use md5 with base64. https://go.dev/play/p/UY0ng2AefFC

Example
str := "hello"
key := "12345"

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

6DQwbquJLYclJdSRinpjmg==

func HmacSha1

func HmacSha1(str, key string) string

HmacSha1 return the hmac hash of string use sha1. Play: https://go.dev/play/p/1UI4oQ4WXKM

Example
str := "hello"
key := "12345"

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

5c6a9db0cccb92e36ed0323fd09b7f936de9ace0

func HmacSha1WithBase64 added in v2.2.4

func HmacSha1WithBase64(str, key string) string

HmacSha1WithBase64 return the hmac hash of string use sha1 with base64. Play: https://go.dev/play/p/47JmmGrnF7B

Example
str := "hello"
key := "12345"

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

XGqdsMzLkuNu0DI/0Jt/k23prOA=

func HmacSha256

func HmacSha256(str, key string) string

HmacSha256 return the hmac hash of string use sha256. Play: https://go.dev/play/p/HhpwXxFhhC0

Example
str := "hello"
key := "12345"

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

315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75

func HmacSha256WithBase64 added in v2.2.4

func HmacSha256WithBase64(str, key string) string

HmacSha256WithBase64 return the hmac hash of string use sha256 with base64. Play: https://go.dev/play/p/EKbkUvPTLwO

Example
str := "hello"
key := "12345"

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

MVu5PE6YmGK6Ccti4F1zpfN2yzbw14btqwwyDQWf3nU=

func HmacSha512

func HmacSha512(str, key string) string

HmacSha512 return the hmac hash of string use sha512. Play: https://go.dev/play/p/59Od6m4A0Ud

Example
str := "hello"
key := "12345"

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

dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc

func HmacSha512WithBase64 added in v2.2.4

func HmacSha512WithBase64(str, key string) string

HmacSha512WithBase64 return the hmac hash of string use sha512 with base64. Play: https://go.dev/play/p/c6dSe3E2ydU

Example
str := "hello"
key := "12345"

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

3Y8SkKndI9NU4lJtmi6c6M///dN8syCADRxsE9Lvw2Mog3ahlsVFja9T+OGqa0Wm2FYwPVwKIGS/+XhYYdSM/A==

func Md5Byte added in v2.2.3

func Md5Byte(data []byte) string

Md5Byte return the md5 string of byte slice. Play: https://go.dev/play/p/suraalH8lyC

Example
md5Str := Md5Byte([]byte{'a'})
fmt.Println(md5Str)
Output:

0cc175b9c0f1b6a831c399e269772661

func Md5ByteWithBase64 added in v2.2.4

func Md5ByteWithBase64(data []byte) string

Md5ByteWithBase64 return the md5 string of byte slice with base64. Play: https://go.dev/play/p/Tcb-Z7LN2ax

Example
md5Str := Md5ByteWithBase64([]byte("hello"))
fmt.Println(md5Str)
Output:

XUFAKrxLKna5cZ2REBfFkg==

func Md5File

func Md5File(filename string) (string, error)

Md5File return the md5 value of file.

func Md5String

func Md5String(s string) string

Md5String return the md5 value of string. Play: https://go.dev/play/p/1bLcVetbTOI

Example
md5Str := Md5String("hello")
fmt.Println(md5Str)
Output:

5d41402abc4b2a76b9719d911017c592

func Md5StringWithBase64 added in v2.2.4

func Md5StringWithBase64(s string) string

Md5StringWithBase64 return the md5 value of string with base64. Play: https://go.dev/play/p/Lx4gH7Vdr5_y

Example
md5Str := Md5StringWithBase64("hello")
fmt.Println(md5Str)
Output:

XUFAKrxLKna5cZ2REBfFkg==

func RsaDecrypt

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

RsaDecrypt decrypt data with ras algorithm. Play: https://go.dev/play/p/rDqTT01SPkZ

Example
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
	return
}

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

fmt.Println(string(decrypted))
Output:

hello

func RsaDecryptOAEP added in v2.2.7

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

RsaDecryptOAEP decrypts the data with RSA-OAEP. Play: https://go.dev/play/p/sSVmkfENKMz

func RsaEncrypt

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

RsaEncrypt encrypt data with ras algorithm. Play: https://go.dev/play/p/rDqTT01SPkZ

Example
// Create ras private and public pem file
err := GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
	return
}

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

fmt.Println(string(decrypted))
Output:

hello

func RsaEncryptOAEP added in v2.2.7

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

RsaEncryptOAEP encrypts the given data with RSA-OAEP. Play: https://go.dev/play/p/sSVmkfENKMz

Example
pri, pub := GenerateRsaKeyPair(1024)

data := []byte("hello world")
label := []byte("123456")

encrypted, err := RsaEncryptOAEP(data, label, *pub)
if err != nil {
	return
}

decrypted, err := RsaDecryptOAEP([]byte(encrypted), label, *pri)
if err != nil {
	return
}

fmt.Println(string(decrypted))
Output:

hello world

func Sha1

func Sha1(str string) string

Sha1 return the sha1 value (SHA-1 hash algorithm) of string. Play: https://go.dev/play/p/_m_uoD1deMT

Example
result := Sha1("hello")
fmt.Println(result)
Output:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

func Sha1WithBase64 added in v2.2.4

func Sha1WithBase64(str string) string

Sha1WithBase64 return the sha1 value (SHA-1 hash algorithm) of base64 string. Play: https://go.dev/play/p/fSyx-Gl2l2-

Example
result := Sha1WithBase64("hello")
fmt.Println(result)
Output:

qvTGHdzF6KLavt4PO0gs2a6pQ00=

func Sha256

func Sha256(str string) string

Sha256 return the sha256 value (SHA256 hash algorithm) of string. Play: https://go.dev/play/p/tU9tfBMIAr1

Example
result := Sha256("hello")
fmt.Println(result)
Output:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

func Sha256WithBase64 added in v2.2.4

func Sha256WithBase64(str string) string

Sha256WithBase64 return the sha256 value (SHA256 hash algorithm) of base64 string. Play: https://go.dev/play/p/85IXJHIal1k

Example
result := Sha256WithBase64("hello")
fmt.Println(result)
Output:

LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=

func Sha512

func Sha512(str string) string

Sha512 return the sha512 value (SHA512 hash algorithm) of string. Play: https://go.dev/play/p/3WsvLYZxsHa

Example
result := Sha512("hello")
fmt.Println(result)
Output:

9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

func Sha512WithBase64 added in v2.2.4

func Sha512WithBase64(str string) string

Sha512WithBase64 return the sha512 value (SHA512 hash algorithm) of base64 string. Play: https://go.dev/play/p/q_fY2rA-k5I

Example
result := Sha512WithBase64("hello")
fmt.Println(result)
Output:

m3HSJL1i83hdltRq0+o9czGb+8KJDKra4t/3JRlnPKcjI8PZm6XBHXx6zG4UuMXaDEZjR1wuXDre9G9zvN7AQw==

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL