crypto

package
v0.0.0-...-19bee3a Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2021 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadParameters       = errors.New("哈希参数格式错误")
	ErrIncompatibleVersion = errors.New("版本不兼容")
	ErrInvalidHash         = errors.New("编码哈希的格式不正确")
)
View Source
var (
	MaxBig256 = new(big.Int).Set(tt256m1)
	MaxBig63  = new(big.Int).Sub(tt63, big.NewInt(1))
)

Various big integer limit values.

Functions

func AesDecrypt

func AesDecrypt(cipherText, key []byte) (plantText []byte, err error)

func AesEncrypt

func AesEncrypt(plainText, key []byte) []byte

func Base64Decode

func Base64Decode(input string) (data []byte, err error)

Base64Decode 解码

func Base64Encode

func Base64Encode(input []byte) string

Base64Encode 编码

func Base64UrlDecode

func Base64UrlDecode(input string) (data []byte, err error)

Base64Decode 解码

func Base64UrlEncode

func Base64UrlEncode(input []byte) string

Base64Encode 编码

func BigMax

func BigMax(x, y *big.Int) *big.Int

BigMax returns the larger of x or y.

func BigMin

func BigMin(x, y *big.Int) *big.Int

BigMin returns the smaller of x or y.

func BigPow

func BigPow(a, b int64) *big.Int

BigPow returns a ** b as a big integer.

func Byte

func Byte(bigint *big.Int, padlength, n int) byte

Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5

func EccDecrypt

func EccDecrypt(priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, cipherText []byte, salt ...byte) ([]byte, error)

func EccEncrypt

func EccEncrypt(priv *ecdsa.PrivateKey, pub *ecdsa.PublicKey, plantText []byte, salt ...byte) []byte

func Exp

func Exp(base, exponent *big.Int) *big.Int

Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.

Courtesy @karalabe and @chfast

func FirstBitSet

func FirstBitSet(v *big.Int) int

FirstBitSet returns the index of the first 1 bit in v, counting from LSB.

func FromECDSA

func FromECDSA(priv *ecdsa.PrivateKey) []byte

私钥 -> []byte

func FromECDSAPub

func FromECDSAPub(pub *ecdsa.PublicKey) []byte

公钥 -> []byte

func FromsSm2Pub

func FromsSm2Pub(pub *sm2.PublicKey) []byte

func GenerateKey

func GenerateKey(r io.Reader) (prk *ecdsa.PrivateKey, err error)

GenerateKey 生成密钥对

func GenerateRandomBytes

func GenerateRandomBytes(length int) ([]byte, error)

GenerateRandomBytes Generate n number of random bytes

func GenerateSharedSecret

func GenerateSharedSecret(priv crypto.PrivateKey, pub crypto.PublicKey, salt ...byte) ([]byte, error)

GenerateSharedSecret 生成共享密钥

func GetHasher

func GetHasher(name string) hash

func GetRandomString

func GetRandomString(n int, alphabets ...byte) string

func GmHashKey

func GmHashKey(input []byte, keylen int) (key []byte)

func GzipDecode

func GzipDecode(in []byte) (out []byte, err error)

func GzipEncode

func GzipEncode(in []byte) (out []byte, err error)

func Hash32

func Hash32(password, salt []byte) []byte

func HashArgon

func HashArgon(input, salt []byte, keylen uint32) (key []byte)

func HashFile

func HashFile(filename string) (bs []byte, err error)

func HashKey

func HashKey(input []byte, keylen int) (key []byte)

func HashReader

func HashReader(file io.Reader) (bs []byte, err error)

func HashSha

func HashSha(input, salt []byte, keylen int) (key []byte)

func HexDecode

func HexDecode(input string) []byte

func HexEncode

func HexEncode(input []byte) string

func JsDecode

func JsDecode(gmPriv *sm2.PrivateKey, eccPriv *ecdsa.PrivateKey, opt *JsDecodeHelper) (plainText string, err error)

func JsEncode

func JsEncode(gmPriv *sm2.PrivateKey, eccPriv *ecdsa.PrivateKey, gmPub *sm2.PublicKey, eccPub *ecdsa.PublicKey, plainText string) (result map[string]string)

func M

func M(privKey *ecdsa.PrivateKey)

func MakeKeyWithKey

func MakeKeyWithKey(key []byte) (prk *ecdsa.PrivateKey, err error)

MakeKeyWithKey 指定特征后直接生成密钥

func MakeSm2KeyWithKey

func MakeSm2KeyWithKey(key []byte) (prk *sm2.PrivateKey, err error)

MakeKeyWithKey 指定特征后直接生成密钥

func MustParseBig256

func MustParseBig256(s string) *big.Int

MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.

func NewKey

func NewKey() (prk *ecdsa.PrivateKey, err error)

NewKey 随机生成密钥

func NewKeyWithKey

func NewKeyWithKey(key []byte) (prk *ecdsa.PrivateKey)

NewKeyWithKey 指定特征SHA512后生成密钥

func NewSm2Key

func NewSm2Key() (prk *sm2.PrivateKey, err error)

NewKey 随机生成密钥

func NewSm2KeyWithKey

func NewSm2KeyWithKey(key []byte) (prk *sm2.PrivateKey)

NewKeyWithKey 指定特征SHA512后生成密钥

func P256Sm2

func P256Sm2() elliptic.Curve

func P521

func P521() elliptic.Curve

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func Padding

func Padding(ciphertext []byte, blockSize int) []byte

func ParseBig256

func ParseBig256(s string) (*big.Int, bool)

ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func ReadBits

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func SecretboxDecrypt

func SecretboxDecrypt(input []byte, key []byte) (plantText []byte, err error)

Decrypt Decrypt (authenticated) a series of bytes given the secret key

func SecretboxEncrypt

func SecretboxEncrypt(input []byte, key []byte) []byte

func Sha1

func Sha1(input []byte) []byte

func Sha256

func Sha256(input []byte) []byte

func Sha512

func Sha512(input []byte) []byte

func Sign

func Sign(priv *ecdsa.PrivateKey, pt []byte) (sign []byte, err error)

Sign 签名

func Sign2

func Sign2(signer c.Signer, pt []byte, opts c.SignerOpts) (sign []byte, err error)

func SignVer

func SignVer(pub *ecdsa.PublicKey, pt, sign []byte) (ok bool)

SignVer 签名验证

func Sm2Decrypt

func Sm2Decrypt(priv *sm2.PrivateKey, pub *sm2.PublicKey, cipherText []byte, salt ...byte) ([]byte, error)

func Sm2Encrypt

func Sm2Encrypt(priv *sm2.PrivateKey, pub *sm2.PublicKey, plainText []byte, salt ...byte) []byte

func Sm2GenerateKey

func Sm2GenerateKey(r io.Reader) (*sm2.PrivateKey, error)

func Sm4Decrypt

func Sm4Decrypt(cipherText, key []byte) (plantText []byte, err error)

func Sm4Encrypt

func Sm4Encrypt(plainText, key []byte) []byte

func ToECDSA

func ToECDSA(d []byte) (*ecdsa.PrivateKey, error)

[]byte -> 私钥

func ToECDSAPub

func ToECDSAPub(pub []byte) *ecdsa.PublicKey

[]byte -> 公钥

func ToSm2Pub

func ToSm2Pub(pub []byte) *sm2.PublicKey

[]byte -> 公钥

func U256

func U256(x *big.Int) *big.Int

U256 encodes as a 256 bit two's complement number. This operation is destructive.

func UnPadding

func UnPadding(origData []byte) []byte

Types

type HexOrDecimal256

type HexOrDecimal256 big.Int

HexOrDecimal256 marshals big.Int as hex or decimal.

func (*HexOrDecimal256) MarshalText

func (i *HexOrDecimal256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal256) UnmarshalText

func (i *HexOrDecimal256) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type JsDecodeHelper

type JsDecodeHelper struct {
	Time   string
	Once   string
	Cipher string
	Sign   string
	PubKey string
	Hash   string
}

Jump to

Keyboard shortcuts

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