crypto

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: MIT Imports: 20 Imported by: 0

README

crypto-go

Cryptography lib in Go

Usage

go get github.com/AyakuraYuki/crypto-go
import "github.com/AyakuraYuki/crypto-go"
Examples

Need more examples? You can find usage examples of the corresponding tools in the _test.go files within the source code.

  • DES/ECB/PKCS7

    dst, err := crypto.DesECBEncrypt([]byte("apple"), []byte("12345678"), crypto.PaddingPKCS7)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(base64.StdEncoding.EncodeToString(dst))
    
  • AES/CBC/PKCS7

    src := []byte("apple")
    key := []byte("1234567812345678")
    iv := []byte("5678567856785678")
    dst, err := crypto.AesCBCEncrypt(src, key, iv, crypto.PaddingPKCS7)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(base64.StdEncoding.EncodeToString(dst))
    
  • MD5

    src := "hello, world"
    dst := crypto.MD5ToString(src)
    fmt.Println(dst)
    
    src, err := os.ReadFile("/path/to/binary_file")
    if err != nil {
        log.Fatal(err)
    }
    dst := crypto.BinaryMD5ToString(src)
    fmt.Println(dst)
    
  • SHA256

    src := []byte("apple")
    dst := crypto.SHA256(src)
    fmt.Println(hex.EncodeToString(dst))
    
  • SM4/CBC/PKCS7

    src := []byte("apple")
    key := []byte("1234567812345678")
    iv := []byte("5678567856785678")
    dst, err := crypto.SM4CBCEncrypt(src, key, iv, crypto.PaddingPKCS7)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(base64.StdEncoding.EncodeToString(dst))
    

Documentation

Index

Constants

View Source
const PaddingPKCS5 = "PKCS5"
View Source
const PaddingPKCS7 = "PKCS7"
View Source
const PaddingZEROS = "ZEROS"

Variables

View Source
var ErrUnPadding = errors.New("UnPadding error")

Functions

func AesCBCDecrypt

func AesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error)

AesCBCDecrypt AES-CBC decrypt

func AesCBCEncrypt

func AesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error)

AesCBCEncrypt AES-CBC encrypt

func AesECBDecrypt

func AesECBDecrypt(src, key []byte, padding string) ([]byte, error)

AesECBDecrypt AES-ECB decrypt

func AesECBEncrypt

func AesECBEncrypt(src, key []byte, padding string) ([]byte, error)

AesECBEncrypt AES-ECB encrypt

func BinaryMD5

func BinaryMD5(src []byte) []byte

BinaryMD5 calculates the md5 hash of a binary

func BinaryMD5ToString

func BinaryMD5ToString(src []byte) string

BinaryMD5ToString calculates the md5 hash of a binary, returns hex string

func CBCDecrypt

func CBCDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error)

func CBCEncrypt

func CBCEncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error)

func CTRDecrypt

func CTRDecrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error)

func CTREncrypt

func CTREncrypt(block cipher.Block, src, iv []byte, padding string) ([]byte, error)

func DesCBCDecrypt

func DesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error)

DesCBCDecrypt DES-CBC decrypt

func DesCBCEncrypt

func DesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error)

DesCBCEncrypt DES-CBC encrypt

func DesECBDecrypt

func DesECBDecrypt(src, key []byte, padding string) ([]byte, error)

DesECBDecrypt DES-ECB decrypt

func DesECBEncrypt

func DesECBEncrypt(src, key []byte, padding string) ([]byte, error)

DesECBEncrypt DES-ECB encrypt

func ECBDecrypt

func ECBDecrypt(block cipher.Block, src []byte, padding string) ([]byte, error)

func ECBEncrypt

func ECBEncrypt(block cipher.Block, src []byte, padding string) ([]byte, error)

func GCMDecrypt

func GCMDecrypt(block cipher.Block, src, nonce, A []byte, padding string) (dst []byte, err error)

func GCMEncrypt

func GCMEncrypt(block cipher.Block, src, nonce, A []byte, padding string) (dst []byte, err error)

func HmacSHA1

func HmacSHA1(src, key []byte) []byte

func HmacSHA256

func HmacSHA256(src, key []byte) []byte

func HmacSHA384

func HmacSHA384(src, key []byte) []byte

func HmacSHA512

func HmacSHA512(src, key []byte) []byte

func HmacSHA256224

func HmacSHA256224(src, key []byte) []byte

func HmacSHA512224

func HmacSHA512224(src, key []byte) []byte

func HmacSHA512256

func HmacSHA512256(src, key []byte) []byte

func MD5

func MD5(str string) []byte

MD5 calculates the md5 hash of a string

func MD5ToString

func MD5ToString(str string) string

MD5ToString calculates the md5 hash of a string, returns hex string

func NewAesCipher

func NewAesCipher(key []byte) (cipher.Block, error)

NewAesCipher creates a new AES cipher.Block

func NewDesCipher

func NewDesCipher(key []byte) (cipher.Block, error)

NewDesCipher creates a new DES cipher.Block

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a cipher.BlockMode which decrypts in Electronic Codebook mode, using the given cipher.Block.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a cipher.BlockMode which encrypts in Electronic Codebook mode, using the given cipher.Block.

func PKCS5Padding

func PKCS5Padding(src []byte, blockSize int) []byte

func PKCS5UnPadding

func PKCS5UnPadding(src []byte) ([]byte, error)

func PKCS7Padding

func PKCS7Padding(src []byte, blockSize int) []byte

func PKCS7UnPadding

func PKCS7UnPadding(src []byte) ([]byte, error)

func Padding

func Padding(padding string, src []byte, blockSize int) []byte

func RC4

func RC4(src, key []byte) ([]byte, error)

func RC4FromBase64

func RC4FromBase64(src string, key []byte) ([]byte, error)

func RC4FromHex

func RC4FromHex(src string, key []byte) ([]byte, error)

func RC4FromURLBase64

func RC4FromURLBase64(src string, key []byte) ([]byte, error)

func RC4ToBase64

func RC4ToBase64(src, key []byte) (string, error)

func RC4ToHex

func RC4ToHex(src, key []byte) (string, error)

func RC4ToURLBase64

func RC4ToURLBase64(src, key []byte) (string, error)

func RSADecrypt

func RSADecrypt(src, priKey []byte) ([]byte, error)

RSADecrypt RSA decrypt by private key

func RSAEncrypt

func RSAEncrypt(src, pubKey []byte) ([]byte, error)

RSAEncrypt RSA encrypt by public key

func RSAGenerateKey

func RSAGenerateKey(bits int) (priKey []byte, err error)

RSAGenerateKey generates RSA private key, returns bytes

func RSAGeneratePublicKey

func RSAGeneratePublicKey(priKey []byte) (pubKey []byte, err error)

RSAGeneratePublicKey generates RSA public keys, returns bytes

func RSASign

func RSASign(src, priKey []byte, hash crypto.Hash) ([]byte, error)

RSASign RSA sign by private key

func RSAVerify

func RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error

RSAVerify RSA verify sign by public key

func SHA1

func SHA1(src []byte) []byte

func SHA3

func SHA3(src []byte) []byte

func SHA224

func SHA224(src []byte) []byte

func SHA256

func SHA256(src []byte) []byte

func SHA384

func SHA384(src []byte) []byte

func SHA512

func SHA512(src []byte) []byte

func SHA3224

func SHA3224(src []byte) []byte

func SHA3256

func SHA3256(src []byte) []byte

func SHA3384

func SHA3384(src []byte) []byte

func SHA512224

func SHA512224(src []byte) []byte

func SHA512256

func SHA512256(src []byte) []byte

func SM4CBCDecrypt

func SM4CBCDecrypt(src, key, iv []byte, padding string) (dst []byte, err error)

SM4CBCDecrypt SM4 CBC decrypt

func SM4CBCEncrypt

func SM4CBCEncrypt(src, key, iv []byte, padding string) (dst []byte, err error)

SM4CBCEncrypt SM4 CBC encrypt

func SM4CTRDecrypt

func SM4CTRDecrypt(src, key, iv []byte, padding string) (dst []byte, err error)

SM4CTRDecrypt SM4 CTR decrypt

func SM4CTREncrypt

func SM4CTREncrypt(src, key, iv []byte, padding string) (dst []byte, err error)

SM4CTREncrypt SM4 CTR encrypt

func SM4ECBDecrypt

func SM4ECBDecrypt(src, key []byte, padding string) (dst []byte, err error)

SM4ECBDecrypt SM4 ECB decrypt

func SM4ECBEncrypt

func SM4ECBEncrypt(src, key []byte, padding string) (dst []byte, err error)

SM4ECBEncrypt SM4 ECB encrypt

func SM4GCMDecrypt

func SM4GCMDecrypt(src, key, iv, A []byte, padding string) (dst []byte, err error)

SM4GCMDecrypt SM4 GCM decrypt

func SM4GCMEncrypt

func SM4GCMEncrypt(src, key, iv, A []byte, padding string) (dst []byte, err error)

SM4GCMEncrypt SM4 GCM encrypt

func TripleDesCBCDecrypt

func TripleDesCBCDecrypt(src, key, iv []byte, padding string) ([]byte, error)

TripleDesCBCDecrypt 3DES-CBC decrypt

func TripleDesCBCEncrypt

func TripleDesCBCEncrypt(src, key, iv []byte, padding string) ([]byte, error)

TripleDesCBCEncrypt 3DES-CBC encrypt

func TripleDesECBDecrypt

func TripleDesECBDecrypt(src, key []byte, padding string) ([]byte, error)

TripleDesECBDecrypt 3DES-ECB decrypt

func TripleDesECBEncrypt

func TripleDesECBEncrypt(src, key []byte, padding string) ([]byte, error)

TripleDesECBEncrypt 3DES-ECB encrypt

func UnPadding

func UnPadding(padding string, src []byte) ([]byte, error)

func ZerosPadding

func ZerosPadding(src []byte, blockSize int) []byte

func ZerosUnPadding

func ZerosUnPadding(src []byte) ([]byte, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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