cryptor

package
v0.0.0-...-d1e1ff0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 19 Imported by: 1

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 AesCbcDecryptBase64

func AesCbcDecryptBase64(encrypted, key string) string

AesCbcDecryptBase64 decrypt data with key use AES CBC algorithm len(key) should be 16, 24 or 32.

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 AesCbcEncryptBase64

func AesCbcEncryptBase64(data, key string) string

AesCbcEncryptBase64 decrypt data with key use AES CBC base64 algorithm len(key) should be 16, 24 or 32.

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 AesCfbDecryptBase64

func AesCfbDecryptBase64(encrypted, key string) string

AesCfbDecryptBase64 decrypt data with key use AES CFB algorithm len(encrypted) should be great than 16, len(key) should be 16, 24 or 32.

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 AesCfbEncryptBase64

func AesCfbEncryptBase64(data, key string) string

AesCfbEncryptBase64 encrypt data with key use AES CFB algorithm len(key) should be 16, 24 or 32.

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 AesOfbDecryptBase64

func AesOfbDecryptBase64(encrypted, key string) string

AesOfbDecryptBase64 decrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32.

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 AesOfbEncryptBase64

func AesOfbEncryptBase64(data, key string) string

AesOfbEncryptBase64 encrypt data with key use AES OFB algorithm len(key) should be 16, 24 or 32.

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 16, 24 or 32. 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 HmacMd5

func HmacMd5(data, 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 HmacSha1

func HmacSha1(data, 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 HmacSha256

func HmacSha256(data, 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 HmacSha512

func HmacSha512(data, 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 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
str := "hello"

md5Str := Md5String(str)

fmt.Println(md5Str)
Output:

5d41402abc4b2a76b9719d911017c592

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 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 Sha1

func Sha1(data string) string

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

Example
str := "hello"

result := Sha1(str)

fmt.Println(result)
Output:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

func Sha256

func Sha256(data string) string

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

Example
str := "hello"

result := Sha256(str)

fmt.Println(result)
Output:

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

func Sha512

func Sha512(data string) string

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

Example
str := "hello"

result := Sha512(str)

fmt.Println(result)
Output:

9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

Types

This section is empty.

Directories

Path Synopsis
Package morse implements morse encoding, fork from https://github.com/martinlindhe/morse
Package morse implements morse encoding, fork from https://github.com/martinlindhe/morse
Package openssl for key certificate management
Package openssl for key certificate management
from https://github.com/hashicorp/vault/blob/v1.13.1/shamir
from https://github.com/hashicorp/vault/blob/v1.13.1/shamir

Jump to

Keyboard shortcuts

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