go-cryptobin

module
v1.0.5006 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0

README

go-cryptobin

go-cryptobin 是 go 的常用加密解密库

中文 | English

项目介绍

  • go-cryptobin 包括常用的对称加密和非对称加密及签名验证
  • 对称加密解密(Aes/Des/TripleDes/SM4/Tea/Twofish/Xts)
  • 对称加密解密模式(ECB/CBC/PCBC/CFB/NCFB/OFB/NOFB/CTR/GCM/CCM)
  • 对称加密解密补码(NoPadding/ZeroPadding/PKCS5Padding/PKCS7Padding/X923Padding/ISO10126Padding/ISO97971Padding/ISO7816_4Padding/PBOC2Padding/TBCPadding/PKCS1Padding)
  • 非对称加密解密(RSA/SM2/ElGamal)
  • 非对称签名验证(RSA/RSA-PSS/DSA/ECDSA/EC-GDSA/EdDSA/SM2/ElGamal/ED448/Gost)
  • 默认 Aes, ECB, NoPadding

环境要求

  • Go >= 1.20

下载安装

go get -u github.com/deatil/go-cryptobin

开始使用

package main

import (
    "fmt"

    "github.com/deatil/go-cryptobin/cryptobin/crypto"
)

func main() {
    // 加密
    cypten := crypto.
        FromString("useData").
        SetKey("dfertf12dfertf12").
        SetIv("dfertf12dfertf12").
        Aes().
        CBC().
        PKCS7Padding().
        Encrypt().
        ToBase64String()

    // 解密
    cyptde := crypto.
        FromBase64String(cypten).
        SetKey("dfertf12dfertf12").
        SetIv("dfertf12dfertf12").
        Aes().
        CBC().
        PKCS7Padding().
        Decrypt().
        ToString()

    fmt.Println("加密结果:", cypten)
    fmt.Println("解密结果:", cyptde)
}

结构说明

  • 默认方式 Aes, ECB, NoPadding。默认没有用补码, 默认输入值需为 16 长度倍数, 不是正确长度可以使用其他补码
// 加密数据
cypt := crypto.
    FromString("useData5useData5").
    SetKey("dfertf12dfertf12").
    Encrypt().
    ToBase64String()

// 解密数据
cyptde := crypto.
    FromBase64String("eZf7c3fcwKlmrqogiEHbJg==").
    SetKey("dfertf12dfertf12").
    Decrypt().
    ToString()
  • 结构说明
// 使用代码
// 注意: 设置密码,加密类型,加密模式,补码方式 在 操作类型 之前, 可以调换顺序
ret := crypto.
    FromString("string"). // 数据来源, 待加密数据/待解密数据
    SetKey("key_string"). // 设置密码
    SetIv("iv_string").   // 设置向量
    Aes().                // 加密类型
    CBC().                // 加密模式
    PKCS7Padding().       // 补码方式
    Encrypt().            // 操作类型, 加密或者解密
    ToBase64String()      // 返回结果数据类型

可用方法

  • 数据来源: FromBytes(data []byte), FromString(data string), FromBase64String(data string), FromHexString(data string)

  • 设置密码: SetKey(data string), WithKey(key []byte)

  • 设置向量: SetIv(data string), WithIv(iv []byte)

  • 加密类型: Aes(), Des(), TripleDes(), Twofish(), Blowfish(), Tea(rounds ...int), Xtea(), Cast5(), RC4(), Idea(), SM4(), Chacha20(counter ...uint32), Chacha20poly1305(additional ...[]byte), Xts(cipher string, sectorNum uint64)

  • 加密模式: ECB(), CBC(), PCBC(), CFB(), OFB(), CTR(), GCM(additional ...[]byte), CCM(additional ...[]byte)

  • 补码方式: NoPadding(), ZeroPadding(), PKCS5Padding(), PKCS7Padding(), X923Padding(), ISO10126Padding(), ISO7816_4Padding(),ISO97971Padding(),PBOC2Padding(), TBCPadding(), PKCS1Padding(bt ...string)

  • 操作类型: Encrypt(), Decrypt(), FuncEncrypt(f func(Cryptobin) Cryptobin), FuncDecrypt(f func(Cryptobin) Cryptobin)

  • 返回数据类型: ToBytes(), ToString(), ToBase64String(), ToHexString()

  • 更多信息可以查看 文档

开源协议

  • 本软件包遵循 Apache2 开源协议发布,在保留本软件包版权的情况下提供个人及商业免费使用。

版权

Directories

Path Synopsis
ber
cipher
ascon
Package ascon implements the ASCON AEAD cipher.
Package ascon implements the ASCON AEAD cipher.
des
e2
hc
kcipher2
Package kcipher2 implements the KCipher-2 stream cipher
Package kcipher2 implements the KCipher-2 stream cipher
lea
misty1
Package misty1 implements the MISTY1 cipher
Package misty1 implements the MISTY1 cipher
rc2
rc5
rc6
sm4
spritz
Package spritz implements the Spritz stream-cipher
Package spritz implements the Spritz stream-cipher
threefish
Package threefish implements the Threefish tweakable block cipher.
Package threefish implements the Threefish tweakable block cipher.
twine
Package twine implements the TWINE lightweight block cipher
Package twine implements the TWINE lightweight block cipher
cryptobin
ca
dsa
ed448
// 生成公钥私钥 / CreateKey: obj := ed448.
// 生成公钥私钥 / CreateKey: obj := ed448.
rsa
sm2
key
elliptic
base_elliptic
Package `base_elliptic` implements Elliptic curves over binary fields
Package `base_elliptic` implements Elliptic curves over binary fields
bign/curve/fiat
Code generated by generate.go.
Code generated by generate.go.
bitcurves
Package bitelliptic implements several Koblitz elliptic curves over prime fields.
Package bitelliptic implements several Koblitz elliptic curves over prime fields.
secp256k1
Package secp256k1 implements the standard secp256k1 elliptic curve over prime fields.
Package secp256k1 implements the standard secp256k1 elliptic curve over prime fields.
gm
sm2
sm9
zuc
hash
cmac/aes
Package aes implements the CMAC MAC with the AES.
Package aes implements the CMAC MAC with the AES.
md2
pmac
PMAC message authentication code, defined in http://web.cs.ucdavis.edu/~rogaway/ocb/pmac.pdf
PMAC message authentication code, defined in http://web.cs.ucdavis.edu/~rogaway/ocb/pmac.pdf
pmac/aes
Package aes implements the PMAC MAC with the AES.
Package aes implements the PMAC MAC with the AES.
rabin
Package rabin implements Rabin hashing (fingerprinting).
Package rabin implements Rabin hashing (fingerprinting).
sm3
kdf
implements the GOST 28147 OFB counter mode (GCTR).
implements the GOST 28147 OFB counter mode (GCTR).
ccm
eax
mgm
ocb
siv
passhash
ber
pubkey
dsa
ed448
Package ed448 implements the Ed448 signature algorithm defined in RFC 8032.
Package ed448 implements the Ed448 signature algorithm defined in RFC 8032.
lms
rsa
x25519
Package x25519 implements the X25519 Elliptic Curve Diffie-Hellman algorithm.
Package x25519 implements the X25519 Elliptic Curve Diffie-Hellman algorithm.
x448
Package x448 implements the X448 Elliptic Curve Diffie-Hellman algorithm.
Package x448 implements the X448 Elliptic Curve Diffie-Hellman algorithm.
rand
tool
pem
xor

Jump to

Keyboard shortcuts

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