security

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 21 Imported by: 0

README

securityLib

security library for encrypt and decrypt.

Documentation

Index

Constants

View Source
const (
	// TripleDESKeyingOption1 means all three keys are independent
	TripleDESKeyingOption1 = 24
	// TripleDESKeyingOption2 means K1 and K2 are independent, and K3 = K1
	TripleDESKeyingOption2 = 16
	// TripleDESKeyingOption3 means All three keys are identical, i.e. K1 = K2 = K3
	TripleDESKeyingOption3 = 8
)

Variables

View Source
var (
	// ErrDivisionByZero returns because the input may cause division by zero
	ErrDivisionByZero = errors.New("division by zero")
	// ErrKeyLength returned because input key length is not qualified
	ErrKeyLength = errors.New("key length error, must be 16 or 24")
)
View Source
var (
	// ErrHashTypeNotAllowed represents that input has type is not allowed
	ErrHashTypeNotAllowed = errors.New("Hash algorithm must be one of md5, sha1, sha256, sha512")
	// ErrNotPEMEncoded represents that key are not encoded by PEM
	ErrNotPEMEncoded = errors.New("Not PEM-encoded")
	// ErrUnkownKeyType represents that key file type are not RSA
	ErrUnkownKeyType = errors.New("Unknown key type")
	// ErrNotRSAPublicKey represents that key is not an RSA public key format
	ErrNotRSAPublicKey = errors.New("Not an RSA public format")
)

Functions

func AESCbcPkcs7PaddingDecrypt

func AESCbcPkcs7PaddingDecrypt(crypted, key []byte) ([]byte, error)

AESCbcPkcs7PaddingDecrypt AES decrypttion with CBC and PKCS7Padding Key length: 16, 24, 32 bytes => AES-128, AES-192, AES-256

func AESCbcPkcs7PaddingEncrypt

func AESCbcPkcs7PaddingEncrypt(origData, key []byte) ([]byte, error)

AESCbcPkcs7PaddingEncrypt AES encrypttion with CBC and PKCS7Padding

func AESCfbDecrypt

func AESCfbDecrypt(ciphertext, key []byte) (plaintext []byte, err error)

AESCfbDecrypt returns the decrypt string of the crypted string by CFB AES. According by https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

func AESCfbEncrypt

func AESCfbEncrypt(text, key []byte) (ciphertext []byte, err error)

AESCfbEncrypt returns the encrypt string of the crypted string by CFB AES. According by https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

func AESEcbPkcs7PaddingDecrypt

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

AesECBDecrypt

func AESEcbPkcs7PaddingEncrypt

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

AesECBEncrypt

func AESPKCS5Padding

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

AESPKCS5Padding PKCS5padding for AES

func AESPKCS5UnPadding

func AESPKCS5UnPadding(origData []byte) []byte

AESPKCS5UnPadding PKCS5UnPadding for AES

func BlowfishDecrypt

func BlowfishDecrypt(dataToDecrypt, key []byte) []byte

BlowfishDecrypt blowfish decryption

func BlowfishEncrypt

func BlowfishEncrypt(dataToEncrypt, key []byte) []byte

BlowfishEncrypt blowfish encryption

func CaesarDecrypt

func CaesarDecrypt(n int, data []byte) ([]byte, error)

CaesarDecrypt but it can handles numbers and any ASCII codes. recording by: https://en.wikipedia.org/wiki/Caesar_cipher

func CaesarEncrypt

func CaesarEncrypt(n int, data []byte) ([]byte, error)

CaesarEncrypt but it can handles numbers and any ASCII codes. recording by: https://en.wikipedia.org/wiki/Caesar_cipher

func DESDecrypt

func DESDecrypt(crypted, key []byte) ([]byte, error)

with zero IV

func DESEncrypt

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

with zero IV

func DESPKCS5Padding

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

func DESPKCS5UnPadding

func DESPKCS5UnPadding(origData []byte) []byte

func GenerateAES256RandomBase64String

func GenerateAES256RandomBase64String() (string, error)

GenerateAES256RandomBase64String returns a base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateAES256RandomBytes

func GenerateAES256RandomBytes() ([]byte, error)

GenerateAES256RandomBites returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRSAKeyPairToFile

func GenerateRSAKeyPairToFile(privateKeyFilepath, publicKeyFilepath string, keySize int) error

GenerateRSAKeyPairToFile creates random RSA private and public key pairs then saves them in files.

func KeyTo24Padding

func KeyTo24Padding(key []byte) ([]byte, error)

KeyTo24Padding returns 24 bytes length of key.

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(data []byte, blockSize int) ([]byte, error)

func PKCS5UnPadding

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

PKCS5UnPadding returns a list of bytes which has removed padding bytes in it.

func PKCS7Padding

func PKCS7Padding(b []byte, blocksize int) ([]byte, error)

AESPKCS7Padding right-pads the given byte slice with 1 to n bytes, where n is the block size. The size of the result is x times n, where x is at least 1.

func PKCS7UnPadding

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

AESPKCS7UnPadding validates and unpads data from the given bytes slice. The returned value will be 1 to n bytes smaller depending on the amount of padding, where n is the block size.

func ReadRSAPrivateKeyFromByte

func ReadRSAPrivateKeyFromByte(privateKey []byte) (*rsa.PrivateKey, error)

ReadRSAPrivateKeyFromByte returns the RSA private key from byte array.

func ReadRSAPrivateKeyFromFile

func ReadRSAPrivateKeyFromFile(privateKeyFilepath string) (*rsa.PrivateKey, error)

ReadRSAPrivateKeyFromFile returns the RSA private key from file.

func ReadRSAPublicKeyFromByte

func ReadRSAPublicKeyFromByte(publicKey []byte) (*rsa.PublicKey, error)

ReadRSAPublicKeyFromByte returns the RSA public key from byte array.

func ReadRSAPublicKeyFromFile

func ReadRSAPublicKeyFromFile(publicKeyFilepath string) (*rsa.PublicKey, error)

ReadRSAPublicKeyFromFile returns the RSA public key from file.

func Sha256HexEecode

func Sha256HexEecode(s string) string

Sha256HexEecode returns a hex encoded string of sha256 hashed string.

func TripleDesEcbDecrypt

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

TripleDesEcbDecrypt returns the decrypt string of the crypted string by ECB 3DES. The input key must bigger or equal to 8 bytes. According by https://en.wikipedia.org/wiki/Triple_DES to implement.

func TripleDesEcbEncrypt

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

TripleDesEcbEncrypt returns the encrypt string of the plantext by ECB 3DES. The input key must bigger or equal to 8 bytes and must be a multiple of 8. According by https://en.wikipedia.org/wiki/Triple_DES to implement.

func ZerosPadding

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

zeroPadding

func ZerosUnPadding

func ZerosUnPadding(src []byte) []byte

Types

type RSA

type RSA struct {
	// contains filtered or unexported fields
}

RSA is the RSA hash algorithm instance.

func NewRSA

func NewRSA(hashType string) (*RSA, error)

NewRSA returns the RSA instance and the encrypt, decrypt type will depends on input.

func (*RSA) Decrypt

func (r *RSA) Decrypt(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)

Decrypt decrypts the RSA encrypted text.

func (*RSA) DecryptByReadRSAPrivateKeyFromFile

func (r *RSA) DecryptByReadRSAPrivateKeyFromFile(privateKeyFilepath string, ciphertext []byte) ([]byte, error)

DecryptByReadRSAPrivateKeyFromFile decodes the RSA encrypted text by reading the private key from file.

func (*RSA) Encrypt

func (r *RSA) Encrypt(publicKey *rsa.PublicKey, plaintext []byte) ([]byte, error)

Encrypt encrypts the plain text by RSA.

Jump to

Keyboard shortcuts

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