gocrypto

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2024 License: MIT Imports: 19 Imported by: 0

README

gocrypto

Commonly used one-way encryption, symmetric encryption and decryption, asymmetric encryption and decryption libraries, including hash, aes, des, rsa.


Example of use

Hash one-way encryption

    import "gitee.com/the_sun_light/common_pkg/gocrypto"

    var hashRawData = []byte("hash_abcdefghijklmnopqrstuvwxyz0123456789")

    // independent hash functions
    gocrypto.Md5(hashRawData)
    gocrypto.Sha1(hashRawData)
    gocrypto.Sha256(hashRawData)
    gocrypto.Sha512(hashRawData)

    // hash collection, specify the execution of the corresponding hash function
    // according to the hash type
    gocrypto.Hash(crypto.MD5, hashRawData)
    gocrypto.Hash(crypto.SHA3_224, hashRawData)
    gocrypto.Hash(crypto.SHA256, hashRawData)
    gocrypto.Hash(crypto.SHA3_224, hashRawData)
    gocrypto.Hash(crypto.BLAKE2s_256, hashRawData)

###Password hash and checksum with salt

The password registered by the user is stored in the database through hash, and the password registered is compared with the hash value to judge whether the password is correct all the time, so as to ensure that only the user knows the plaintext of the password.

    import "gitee.com/the_sun_light/common_pkg/gocrypto"

    pwd := "123"

    // hash
    hashStr, err := gocrypto.HashAndSaltPassword(pwd)
    if err != nil {
        return err
    }

    // check password
    ok := gocrypto.VerifyPassword(pwd, hashStr)
    if !ok {
        return errors.New("passwords mismatch")
    }

AES encrypt and decrypt

AES (Advanced Encryption Standard) Advanced Encryption Standard, designed to replace DES, has four packet encryption modes: ECB CBC CFB CTR.

There are four functions AesEncrypt, AesDecrypt, AesEncryptHex, AesDecryptHex.

    import "gitee.com/the_sun_light/common_pkg/gocrypto"

    var (
        aesRawData = []byte("aes_abcdefghijklmnopqrstuvwxyz0123456789")
        aesKey     = []byte("aesKey0123456789aesKey0123456789")
    )

    // AesEncrypt and AesDecrypt have default values for their arguments:
    // default mode is ECB, can be modified to CBC CTR CFB 
    // default key length is 16, which can be modified to 24 32

    // default mode is ECB, default key length is 16
    cypherData, _ := gocrypto.AesEncrypt(aesRawData) // encrypt
    raw, _ := gocrypto.AesDecrypt(cypherData) // decrypt, return to original

    // mode is ECB, key length is 32
    cypherData, _ := gocrypto.AesEncrypt(aesRawData, gocrypto.WithAesKey(aesKey))  // encrypt
    raw, _ := gocrypto.AesDecrypt(cypherData, gocrypto.WithAesKey(aesKey)) // decrypt

    // mode is CTR, default key length is 16
    cypherData, _ := gocrypto.AesEncrypt(aesRawData, gocrypto.WithAesModeCTR())  // encrypt
    raw, _ := gocrypto.AesDecrypt(cypherData, gocrypto.WithAesModeCTR())  // decrypt

    // mode is CBC, key length is 32
    cypherData, _ := gocrypto.AesEncrypt(aesRawData, gocrypto.WithAesModeECB(), gocrypto.WithAesKey(aesKey)) // encrypt
    raw, _ := gocrypto.AesDecrypt(cypherData, gocrypto.WithAesModeECB(), gocrypto.WithAesKey(aesKey))   // decrypt


    // AesEncryptHex and AesDecryptHex functions, the ciphertext of these two functions is transcoded by hex,
    // and used in exactly the same way as AesEncrypt and AesDecrypt.

DES encrypt and decrypt

DES (Data Encryption Standard) data encryption standard, is currently one of the most popular encryption algorithms, there are four packet encryption mode: ECB CBC CFB CTR.

There are four functions DesEncrypt, DesDecrypt, DesEncryptHex, DesDecryptHex.

    import "gitee.com/the_sun_light/common_pkg/gocrypto"

    var (
        desRawData = []byte("des_abcdefghijklmnopqrstuvwxyz0123456789")
        desKey     = []byte("desKey0123456789desKey0123456789")
    )
// PKCS#1
var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
xxxxxx
-----END PUBLIC KEY-----
`)

var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
xxxxxx
-----END RSA PRIVATE KEY-----
`)

   // DesEncrypt and DesDecrypt have default values for their arguments:
   // default mode is ECB, can be modified to CBC CTR CFB 
   // default key length is 16, which can be modified to 24 32

    // default mode is ECB, default key length is 16
    cypherData, _ := gocrypto.DesEncrypt(desRawData) // encrypt
    raw, _ := gocrypto.DesDecrypt(cypherData) // decrypt

    // mode is ECB, key length is 32
    cypherData, _ := gocrypto.DesEncrypt(desRawData, gocrypto.WithDesKey(desKey)) // encrypt
    raw, _ := gocrypto.DesDecrypt(cypherData, gocrypto.WithDesKey(desKey)) // decrypt

    // mode is CTR, default key length is 16
    cypherData, _ := gocrypto.DesEncrypt(desRawData, gocrypto.WithDesModeCTR()) // encrypt
    raw, _ := gocrypto.DesDecrypt(cypherData, gocrypto.WithDesModeCTR()) // decrypt

    // mode is CBC, key length is 32
    cypherData, _ := gocrypto.DesEncrypt(desRawData, gocrypto.WithDesModeECB(), gocrypto.WithDesKey(desKey)) // encrypt
    raw, _ := gocrypto.DesDecrypt(cypherData, gocrypto.WithDesModeECB(), gocrypto.WithDesKey(desKey))        // decrypt


    // DesEncryptHex and DesDecryptHex functions, the ciphertext of these two functions is transcoded by hex,
    // and used in exactly the same way as DesEncrypt and DesDecrypt.

RSA asymmetric encryption and decryption

RSA encryption and decryption

The public key is used for encryption, and the private key is used for decryption. For example, if someone uses the public key to encrypt information and send it to you, you have the private key to decrypt the information content.

There are four functions: RsaEncrypt, RsaDecrypt, RsaEncryptHex, RsaDecryptHex.

    import "gitee.com/the_sun_light/common_pkg/gocrypto"

    var rsaRawData = []byte("rsa_abcdefghijklmnopqrstuvwxyz0123456789")
    // PKCS#1
    var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
xxxxxx
-----END PUBLIC KEY-----
`)

    var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
xxxxxx
-----END RSA PRIVATE KEY-----
`)
	
    // RsaEncrypt and RsaDecrypt have default values for their arguments:
    // default key pair format: PKCS#1, can be modified to PKCS#8

    // default key pair format is PKCS#1
    cypherData, _ := gocrypto.RsaEncrypt(publicKey, rsaRawData) // encrypt
    raw, _ := gocrypto.RsaDecrypt(privateKey, cypherData) // decrypt

    // key pair format is PKCS#8
    cypherData, _ := gocrypto.RsaEncrypt(publicKey, rsaRawData, gocrypto.WithRsaFormatPKCS8()) // encrypt
    raw, _ := gocrypto.RsaDecrypt(privateKey, cypherData, gocrypto.WithRsaFormatPKCS8()) // decrypt


    // RsaEncryptHex and RsaDecryptHex functions, the ciphertext of these two functions is transcoded by hex,
    // and used in exactly the same way as RsaEncrypt and RsaDecrypt.

RSA signature and signature verification

The private key is used to sign, and the public key is used to verify the signature. For example, you sign your identity with the private key, and others verify whether your identity can be trusted through the public key.

There are four functions: RsaSign, RsaVerify, RsaSignBase64, RsaVerifyBase64.

   import "gitee.com/the_sun_light/common_pkg/gocrypto"

    var rsaRawData = []byte("rsa_abcdefghijklmnopqrstuvwxyz0123456789")

    // RsaEncrypt and RsaDecrypt have default values for their arguments:
    // default key pair format is PKCS#1, can be modified to PKCS#8
    // default hash is sha1, can be modified to sha256, sha512

    // default key pair format is PKCS#1, default hash is sha1
    signData, _ := gocrypto.RsaSign(privateKey, rsaRawData) // signature
    err := gocrypto.RsaVerify(publicKey, rsaRawData, signData) // signature verification

    // default key pair format is PKCS#1, hash is sha256
    signData, _ := gocrypto.RsaSign(privateKey, rsaRawData, gocrypto.WithRsaHashTypeSha256()) // signature
    err := gocrypto.RsaVerify(publicKey, rsaRawData, signData, gocrypto.WithRsaHashTypeSha256()) // signature verification

    // key pair format is PKCS#8, default hash is sha1
    signData, _ := gocrypto.RsaSign(privateKey, rsaRawData, gocrypto.WithRsaFormatPKCS8()) // signature
    err := gocrypto.RsaVerify(publicKey, rsaRawData, signData, gocrypto.WithRsaFormatPKCS8()) // signature verification

    // key pair format is PKCS#8, hash is sha512
    signData, _ := gocrypto.RsaSign(privateKey, rsaRawData, gocrypto.WithRsaFormatPKCS8(), gocrypto.WithRsaHashTypeSha512()) // signature
    err := gocrypto.RsaVerify(publicKey, rsaRawData, signData, gocrypto.WithRsaFormatPKCS8(), gocrypto.WithRsaHashTypeSha512()) // signature verification


    // The ciphertext of RsaSignBase64 and RsaVerifyBase64 is base64 transcoded
    // and used exactly the same as RsaSign and RsaVerify.

Documentation

Overview

Package gocrypto is commonly used one-way encryption, symmetric encryption and decryption, asymmetric encryption and decryption libraries, including hash, aes, des, rsa.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AesDecrypt

func AesDecrypt(cipherData []byte, opts ...AesOption) ([]byte, error)

AesDecrypt aes decryption, parameter input un-transcode cipher text

func AesDecryptHex

func AesDecryptHex(cipherStr string, opts ...AesOption) (string, error)

AesDecryptHex aes decryption, parameter input has been transcoded ciphertext string

func AesEncrypt

func AesEncrypt(rawData []byte, opts ...AesOption) ([]byte, error)

AesEncrypt aes encryption, returns ciphertext is not transcoded

func AesEncryptHex

func AesEncryptHex(rawData string, opts ...AesOption) (string, error)

AesEncryptHex aes encryption, the returned ciphertext is transcoded

func DesDecrypt

func DesDecrypt(cipherData []byte, opts ...DesOption) ([]byte, error)

DesDecrypt des decryption, parameter input untranscoded cipher text

func DesDecryptHex

func DesDecryptHex(cipherStr string, opts ...DesOption) (string, error)

DesDecryptHex des decryption, parameter input has been transcoded ciphertext string

func DesEncrypt

func DesEncrypt(rawData []byte, opts ...DesOption) ([]byte, error)

DesEncrypt des encryption, the returned ciphertext is not transcoded

func DesEncryptHex

func DesEncryptHex(rawData string, opts ...DesOption) (string, error)

DesEncryptHex des encrypts and returns a ciphertext that has been transcoded

func Hash

func Hash(hashType crypto.Hash, rawData []byte) (string, error)

Hash commonly used hash sets

func HashAndSaltPassword

func HashAndSaltPassword(password string) (string, error)

HashAndSaltPassword hash password with salt

func Md5

func Md5(rawData []byte) string

Md5 hash

func RsaDecrypt

func RsaDecrypt(privateKey []byte, cipherData []byte, opts ...RsaOption) ([]byte, error)

RsaDecrypt rsa decryption, parameter input untranscoded cipher text

func RsaDecryptHex

func RsaDecryptHex(privateKey []byte, cipherHex string, opts ...RsaOption) (string, error)

RsaDecryptHex rsa decryption, return to original

func RsaEncrypt

func RsaEncrypt(publicKey []byte, rawData []byte, opts ...RsaOption) ([]byte, error)

RsaEncrypt rsa encryption, the returned ciphertext is not transcoded

func RsaEncryptHex

func RsaEncryptHex(publicKey []byte, rawData []byte, opts ...RsaOption) (string, error)

RsaEncryptHex rsa encryption, return hex

func RsaSign

func RsaSign(privateKey []byte, rawData []byte, opts ...RsaOption) ([]byte, error)

RsaSign rsa signature, the returned ciphertext is not transcoded

func RsaSignBase64

func RsaSignBase64(privateKey []byte, rawData []byte, opts ...RsaOption) (string, error)

RsaSignBase64 rsa signature, return base64

func RsaVerify

func RsaVerify(publicKey []byte, rawData []byte, signData []byte, opts ...RsaOption) error

RsaVerify rsa signature verification

func RsaVerifyBase64

func RsaVerifyBase64(publicKey []byte, rawData []byte, signBase64 string, opts ...RsaOption) error

RsaVerifyBase64 rsa signature verification

func Sha1

func Sha1(rawData []byte) string

Sha1 hash

func Sha256

func Sha256(rawData []byte) string

Sha256 hash

func Sha512

func Sha512(rawData []byte) string

Sha512 hash

func VerifyPassword

func VerifyPassword(password string, hashed string) bool

VerifyPassword verify password and ciphertext match

Types

type AesOption

type AesOption func(*aesOptions)

AesOption set the aes options.

func WithAesKey

func WithAesKey(key []byte) AesOption

WithAesKey set aes key

func WithAesModeCBC

func WithAesModeCBC() AesOption

WithAesModeCBC set mode to CBC

func WithAesModeCFB

func WithAesModeCFB() AesOption

WithAesModeCFB set mode to CFB

func WithAesModeCTR

func WithAesModeCTR() AesOption

WithAesModeCTR set mode to CTR

func WithAesModeECB

func WithAesModeECB() AesOption

WithAesModeECB set mode to ECB

type DesOption

type DesOption func(*desOptions)

DesOption set the des options.

func WithDesKey

func WithDesKey(key []byte) DesOption

WithDesKey set des key

func WithDesModeCBC

func WithDesModeCBC() DesOption

WithDesModeCBC set mode to CBC

func WithDesModeCFB

func WithDesModeCFB() DesOption

WithDesModeCFB set mode to CFB

func WithDesModeCTR

func WithDesModeCTR() DesOption

WithDesModeCTR set mode to CTR

func WithDesModeECB

func WithDesModeECB() DesOption

WithDesModeECB set mode to ECB

type RsaOption

type RsaOption func(*rsaOptions)

RsaOption set the rsa options.

func WithRsaFormatPKCS1

func WithRsaFormatPKCS1() RsaOption

WithRsaFormatPKCS1 set format

func WithRsaFormatPKCS8

func WithRsaFormatPKCS8() RsaOption

WithRsaFormatPKCS8 set format

func WithRsaHashType

func WithRsaHashType(hash crypto.Hash) RsaOption

WithRsaHashType set hash type

func WithRsaHashTypeMd5

func WithRsaHashTypeMd5() RsaOption

WithRsaHashTypeMd5 set hash type

func WithRsaHashTypeSha1

func WithRsaHashTypeSha1() RsaOption

WithRsaHashTypeSha1 set hash type

func WithRsaHashTypeSha256

func WithRsaHashTypeSha256() RsaOption

WithRsaHashTypeSha256 set hash type

func WithRsaHashTypeSha512

func WithRsaHashTypeSha512() RsaOption

WithRsaHashTypeSha512 set hash type

Directories

Path Synopsis
Package wcipher is a package to encrypt and decrypt data.
Package wcipher is a package to encrypt and decrypt data.

Jump to

Keyboard shortcuts

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