openssl

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: Apache-2.0 Imports: 16 Imported by: 111

README

Openssl encryption

Go Report Card build Coverage Status Godoc Release

A functions wrapping of OpenSSL library for symmetric and asymmetric encryption and decryption

Installation

The only requirement is the Go Programming Language

go get -u github.com/forgoer/openssl

Usage

AES

The length of the key can be 16/24/32 characters (128/192/256 bits)

AES-ECB:

src := []byte("123456")
key := []byte("1234567890123456")
dst , _ := openssl.AesECBEncrypt(src, key, openssl.PKCS7_PADDING)
fmt.Printf(base64.StdEncoding.EncodeToString(dst))  // yXVUkR45PFz0UfpbDB8/ew==

dst , _ = openssl.AesECBDecrypt(dst, key, openssl.PKCS7_PADDING)
fmt.Println(string(dst)) // 123456

AES-CBC:

src := []byte("123456")
key := []byte("1234567890123456")
iv := []byte("1234567890123456")
dst , _ := openssl.AesCBCEncrypt(src, key, iv, openssl.PKCS7_PADDING)
fmt.Println(base64.StdEncoding.EncodeToString(dst)) // 1jdzWuniG6UMtoa3T6uNLA==

dst , _ = openssl.AesCBCDecrypt(dst, key, iv, openssl.PKCS7_PADDING)
fmt.Println(string(dst)) // 123456
DES

The length of the key must be 8 characters (64 bits).

DES-ECB:

openssl.DesECBEncrypt(src, key, openssl.PKCS7_PADDING)
openssl.DesECBDecrypt(src, key, openssl.PKCS7_PADDING)

DES-CBC:

openssl.DesCBCEncrypt(src, key, iv, openssl.PKCS7_PADDING)
openssl.DesCBCDecrypt(src, key, iv, openssl.PKCS7_PADDING)
3DES

The length of the key must be 24 characters (192 bits).

3DES-ECB:

openssl.Des3ECBEncrypt(src, key, openssl.PKCS7_PADDING)
openssl.Des3ECBDecrypt(src, key, openssl.PKCS7_PADDING)

3DES-CBC:

openssl.Des3CBCEncrypt(src, key, iv, openssl.PKCS7_PADDING)
openssl.Des3CBCDecrypt(src, key, iv, openssl.PKCS7_PADDING)
RSA
openssl.RSAGenerateKey(bits int, out io.Writer)
openssl.RSAGeneratePublicKey(priKey []byte, out io.Writer)

openssl.RSAEncrypt(src, pubKey []byte) ([]byte, error)
openssl.RSADecrypt(src, priKey []byte) ([]byte, error)

openssl.RSASign(src []byte, priKey []byte, hash crypto.Hash) ([]byte, error)
openssl.RSAVerify(src, sign, pubKey []byte, hash crypto.Hash) error
HMAC-SHA
// Sha1 Calculate the sha1 hash of a string
Sha1(str string) []byte

// HmacSha1 Calculate the sha1 hash of a string using the HMAC method
HmacSha1(key string, data string) []byte

// HmacSha1ToString Calculate the sha1 hash of a string using the HMAC method, outputs lowercase hexits
HmacSha1ToString(key string, data string) string

// Sha256 Calculate the sha256 hash of a string
Sha256(str string) []byte

// HmacSha256 Calculate the sha256 hash of a string using the HMAC method
HmacSha256(key string, data string) []byte

// HmacSha256ToString Calculate the sha256 hash of a string using the HMAC method, outputs lowercase hexits
HmacSha256ToString(key string, data string) string

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcomed.

Documentation

Index

Constants

View Source
const PKCS5_PADDING = "PKCS5"
View Source
const PKCS7_PADDING = "PKCS7"
View Source
const ZEROS_PADDING = "ZEROS"

Variables

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

Error indicating an issue during the unpadding process.

Functions

func AesCBCDecrypt

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

AesCBCDecrypt decrypts data using the CBC mode of the AES algorithm.

func AesCBCEncrypt

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

AesCBCEncrypt encrypts data using the CBC mode of the AES algorithm.

func AesECBDecrypt

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

AesECBDecrypt decrypts data using the ECB mode of the AES algorithm.

func AesECBEncrypt

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

AesECBEncrypt encrypts data using the ECB mode of the AES algorithm.

func AesNewCipher added in v1.6.0

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

AesNewCipher creates and returns a new AES cipher block. Automatically pads the key length.

func CBCDecrypt

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

CBCDecrypt decrypts data using the CBC (Cipher Block Chaining) mode.

func CBCEncrypt

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

CBCEncrypt encrypts data using the CBC (Cipher Block Chaining) mode.

func Des3CBCDecrypt

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

Des3CBCDecrypt decrypts data using the CBC mode of the 3DES algorithm.

func Des3CBCEncrypt

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

Des3CBCEncrypt encrypts data using the CBC mode of the 3DES algorithm.

func Des3ECBDecrypt

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

Des3ECBDecrypt decrypts data using the ECB mode of the 3DES algorithm.

func Des3ECBEncrypt

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

Des3ECBEncrypt encrypts data using the ECB mode of the 3DES algorithm.

func DesCBCDecrypt

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

DesCBCDecrypt decrypts data using the CBC mode of the DES algorithm.

func DesCBCEncrypt

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

DesCBCEncrypt encrypts data using the CBC mode of the DES algorithm.

func DesECBDecrypt

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

DesECBDecrypt decrypts data using the ECB mode of the DES algorithm.

func DesECBEncrypt

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

DesECBEncrypt encrypts data using the ECB mode of the DES algorithm.

func DesNewCipher added in v1.6.0

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

DesNewCipher creates and returns a new DES cipher block, adjusting the key length if necessary.

func ECBDecrypt

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

Decrypts data using the ECB (Electronic Codebook) mode.

func ECBEncrypt

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

Encrypts data using the ECB (Electronic Codebook) mode.

func HmacSha1

func HmacSha1(key string, data string) []byte

HmacSha1 Calculate the sha1 hash of a string using the HMAC method

func HmacSha1ToString added in v1.5.0

func HmacSha1ToString(key string, data string) string

HmacSha1ToString Calculate the sha1 hash of a string using the HMAC method, outputs lowercase hexits

func HmacSha256 added in v1.5.0

func HmacSha256(key string, data string) []byte

HmacSha256 Calculate the sha256 hash of a string using the HMAC method

func HmacSha256ToString added in v1.5.0

func HmacSha256ToString(key string, data string) string

HmacSha256ToString Calculate the sha256 hash of a string using the HMAC method, outputs lowercase hexits

func KeyGenerator

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

Generates a key based on the input data and specified block size.

func Md5

func Md5(str string) []byte

Calculates the MD5 hash of a given string.

func Md5ToString added in v1.3.0

func Md5ToString(str string) string

Calculates the MD5 hash of a given string and returns the result as a hexadecimal string.

func NewECBDecrypter

func NewECBDecrypter(b cipher.Block) cipher.BlockMode

NewECBDecrypter returns a BlockMode which decrypts in electronic code book mode, using the given Block.

func NewECBEncrypter

func NewECBEncrypter(b cipher.Block) cipher.BlockMode

NewECBEncrypter returns a BlockMode which encrypts in electronic code book mode, using the given Block.

func PKCS5Padding

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

Applies PKCS5 padding to the input data. In practice, it uses PKCS7 padding.

func PKCS5Unpadding

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

Removes PKCS5 padding from the input data. In practice, it uses PKCS7 unpadding.

func PKCS7Padding

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

Applies PKCS7 padding to the input data.

func PKCS7UnPadding

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

Removes PKCS7 padding from the input data.

func Padding

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

Applies the specified padding scheme to the input data.

func RSADecrypt added in v1.2.0

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

Decrypts data using RSA.

func RSAEncrypt added in v1.2.0

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

Encrypts data using RSA.

func RSAGenerateKey added in v1.2.0

func RSAGenerateKey(bits int, out io.Writer) error

Generates a new RSA private key.

func RSAGeneratePublicKey added in v1.2.0

func RSAGeneratePublicKey(priKey []byte, out io.Writer) error

Generates an RSA public key from a private key.

func RSASign added in v1.2.0

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

Signs data using RSA.

func RSAVerify added in v1.2.0

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

Verifies a signature using RSA.

func SHA1

func SHA1(data []byte) []byte

Computes the SHA-1 hash of the input data.

func Sha1

func Sha1(str string) []byte

Sha1 Calculate the sha1 hash of a string

func Sha256 added in v1.5.0

func Sha256(str string) []byte

Sha256 Calculate the sha256 hash of a string

func UnPadding

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

Removes the specified padding from the input data.

func ZerosPadding

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

Applies zero padding to the input data.

func ZerosUnPadding

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

Removes zero padding from the input data.

Types

This section is empty.

Jump to

Keyboard shortcuts

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