crypto

package module
v0.0.0-...-72c4c7a Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2023 License: MIT Imports: 24 Imported by: 5

README

crypto

GoDoc

Encryption, Hash, Encoding Library.

❤️ A project by txthinking.com

Install

$ go get github.com/txthinking/crypto

Features

  • AES CFB
  • AES CBC
  • AES GCM
  • PKCS
  • HKDF
  • HMAC
  • MD5
  • SHA
  • UTF82GBK
  • GBK2UTF8
  • Encrypt key and value with expire time
  • Encrypt struct
  • RFC3986 URI Encoding
  • RSA SHA256

License

Licensed under The MIT License

Documentation

Index

Constants

View Source
const AES256KeyLength = 32

AES256KeyLength is the length of key for AES 256 crypt

Variables

This section is empty.

Functions

func AESCBCDecrypt

func AESCBCDecrypt(c, k []byte) ([]byte, error)

AESCBCDecrypt decrypt c with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCBCEncrypt

func AESCBCEncrypt(s, k []byte) ([]byte, error)

AESCBCEncrypt encrypt s with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCFBDecrypt

func AESCFBDecrypt(c, k []byte) ([]byte, error)

AESDecrypt decrypt c with given k k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits First 16 bytes of cipher data is the IV.

func AESCFBEncrypt

func AESCFBEncrypt(s, k []byte) ([]byte, error)

AESCFBEncrypt encrypt s with given k. k should be 128/256 bits, otherwise it will append empty data or cut until 256 bits. First 16 bytes of cipher data is the IV.

func AESGCMDecrypt

func AESGCMDecrypt(c, k, n []byte) ([]byte, error)

AESGCMDecrypt decrypt s use k and nonce

func AESGCMEncrypt

func AESGCMEncrypt(s, k, n []byte) ([]byte, error)

AESGCMEncrypt encrypt s use k and nonce

func AESMake256Key

func AESMake256Key(k []byte) []byte

AESMake256Key cut or append empty data on the key and make sure the key lenth equal 32

func CheckHmacSha1

func CheckHmacSha1(message, messageMAC, key []byte) (bool, error)

func CheckHmacSha256

func CheckHmacSha256(message, messageMAC, key []byte) (bool, error)

func GBK2UTF8

func GBK2UTF8(s []byte) ([]byte, error)

GBK2UTF8 transform s from GBK to UTF8 format

func HkdfSha1RandomSalt

func HkdfSha1RandomSalt(secret, info []byte, sl int) (key []byte, salt []byte, err error)

func HkdfSha1WithSalt

func HkdfSha1WithSalt(secret, salt, info []byte) (key []byte, err error)

func HkdfSha256RandomSalt

func HkdfSha256RandomSalt(secret, info []byte, sl int) (key []byte, salt []byte, err error)

func HkdfSha256WithSalt

func HkdfSha256WithSalt(secret, salt, info []byte) (key []byte, err error)

func HmacSha1

func HmacSha1(message, key []byte) ([]byte, error)

func HmacSha256

func HmacSha256(message, key []byte) ([]byte, error)

func MD5

func MD5(s string) (r string)

MD5 encrypt s according to md5 algorithm

func PKCSPadding

func PKCSPadding(c []byte, blockSize int) []byte

PKCS#5 padding is identical to PKCS#7 padding, except that it has only been defined for block ciphers that use a 64-bit (8-byte) block size. In practice the two can be used interchangeably. Common blockSize is aes.BlockSize.

func PKCSUnPadding

func PKCSUnPadding(s []byte) ([]byte, error)

func RSASignWithSHA256

func RSASignWithSHA256(data []byte, key []byte) ([]byte, error)

key is private key, PKCS#8.

func RSASignWithSHA256PKCS1

func RSASignWithSHA256PKCS1(data []byte, key []byte) ([]byte, error)

key is private key, PKCS#1.

func RSAVerifyWithSHA256

func RSAVerifyWithSHA256(data, sign, key []byte) error

key is public key, nil means passed.

func SHA1

func SHA1(s string) (r string)

SHA1 encrypt s according to sha1 algorithm

func SHA256

func SHA256(s string) (r string)

SHA256 encrypt s according to sha256 algorithm

func SHA256Bytes

func SHA256Bytes(s []byte) ([]byte, error)

SHA256 encrypt s according to sha256 algorithm

func URIEscape

func URIEscape(s string) string

func URIUnescape

func URIUnescape(s string) (string, error)

func UTF82GBK

func UTF82GBK(s []byte) ([]byte, error)

UTF82GBK transform s from UTF8 to GBK format

Types

type KV

type KV struct {
	AESKey []byte
}

KV can be used to crypt value by key and give it a lifecycle (s). Can be used in cookies

func (*KV) Decrypt

func (kv *KV) Decrypt(c string, k string, lifecycle int64) (string, error)

Decrypt key, value, lifecycle is second, 0 is no expired time.

func (*KV) DecryptStruct

func (kv *KV) DecryptStruct(c string, v interface{}, k string, lifecycle int64) error

Decrypt key, value, lifecycle is second, 0 is no expired time.

func (*KV) Encrypt

func (kv *KV) Encrypt(k string, v string) (string, error)

Encrypt key, value

func (*KV) EncryptStruct

func (kv *KV) EncryptStruct(k string, v interface{}) (string, error)

JSON it first

Jump to

Keyboard shortcuts

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