crypto

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyMustBePEMEncoded = errors.New("invalid Key: Key must be a PEM encoded PKCS1 or PKCS8 key")
	ErrNotRSAPrivateKey    = errors.New("key is not a valid RSA private key")
	ErrNotRSAPublicKey     = errors.New("key is not a valid RSA public key")
)

Functions

func Decrypt

func Decrypt(passphrase, ciphertext string) string

Decrypt decrypts ciphertext using the passphrase

func DecryptRaw

func DecryptRaw(passphrase, salt, initializationVector, ciphertext []byte) ([]byte, error)

func DecryptToBytes

func DecryptToBytes(passphrase, ciphertext string) []byte

Decrypt decrypts ciphertext using the passphrase. The output is a byte array

func DeriveKey

func DeriveKey(passphrase []byte, salt []byte) ([]byte, []byte)

func Encrypt

func Encrypt(passphrase, plaintext string) string

Encrypt encrypts plain text using passphrase. Returns an Hex String containing Salt, IV and ciphertext.

func EncryptBytes

func EncryptBytes(passphrase, plaintext []byte) string

Encrypt encrypts plain text using passphrase bytes. Returns an Hex String containing Salt, IV and ciphertext.

func ExportRsaPrivateKeyAsPemStr

func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string

func GenerateOTP

func GenerateOTP(length int) (string, error)

Generates OTP using secure random.

func GetDataHash

func GetDataHash(m interface{}) (*[32]byte, error)

func LoadPemFile

func LoadPemFile(filePath string) ([]byte, error)

Loads a Pemfile using the file path from file system

func ParseRsaPrivateKeyFromPemStr

func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error)

func RsaDecryptWithPrivateKey

func RsaDecryptWithPrivateKey(ciphertext []byte, priv *rsa.PrivateKey) []byte

RsaDecryptWithPrivateKey decrypts data with private key

func RsaEncodePrivateKeyToPEM

func RsaEncodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte

RsaEncodePrivateKeyToPEM encodes Private Key from RSA to PEM format

func RsaEncodePublicKeyToPEM

func RsaEncodePublicKeyToPEM(rsaPublicKey *rsa.PublicKey) ([]byte, error)

RsaEncodePublicKeyToPEM take a rsa.PublicKey and return bytes suitable for writing to .pub file returns in the format "ssh-rsa ..."

func RsaEncryptWithPublicKey

func RsaEncryptWithPublicKey(msg []byte, pub *rsa.PublicKey) ([]byte, error)

RsaEncryptWithPublicKey encrypts data with public key

func RsaGenerateAndStore

func RsaGenerateAndStore(bitSize int, privateKeyFile, publicKeyFile string) (*rsa.PrivateKey, error)

func RsaGeneratePrivateKey

func RsaGeneratePrivateKey(bitSize int) (*rsa.PrivateKey, error)

RsaGeneratePrivateKey creates a RSA Private Key of specified byte size

func RsaParsePrivateKeyFromPEM

func RsaParsePrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error)

Parse PEM encoded PKCS1 or PKCS8 private key

func RsaParsePublicKeyFromPEM

func RsaParsePublicKeyFromPEM(key []byte) (*rsa.PublicKey, error)

Parse PEM encoded PKCS1 or PKCS8 public key

func SignRsaMessage

func SignRsaMessage(m interface{}, privateKey *rsa.PrivateKey) (*[]byte, error)

Signs a message (for example an struct) with the given RSA private key

Input:
  The message to sign, it must be serializable to a JSON
  the RSA Privatekey to sign the message
Output:
  The signature as byte array
  the error if there is any error getting the Hash or the signature

func VerifyRsaMessage

func VerifyRsaMessage(m interface{}, publicKey *rsa.PublicKey, signature []byte) bool

Verifies a message (for example an struct) signature using RSA public key

Input:
  The message to verify, it must be serializable to a JSON. Remember to remove the signature from message if you have included it in a field
  the RSA Public key to verify the message
  The signature as byte array
Output:
  true if the signature can be verified with the message or false elsewhere.

Types

type ECDSA

type ECDSA struct {
	PrivateKey *ecdsa.PrivateKey
	PublicKey  *ecdsa.PublicKey
}

func (*ECDSA) DecodeEcdsaPem

func (e *ECDSA) DecodeEcdsaPem(pemEncodedPriv []byte) error

Decodes ECDSA Pem encoded private key

func (*ECDSA) DecodeEcdsaPublicKeyPem

func (e *ECDSA) DecodeEcdsaPublicKeyPem(pemEncodedPub []byte) error

Decodes ECDSA Pem encoded public key

func (*ECDSA) EncodeEcdsaToPem

func (e *ECDSA) EncodeEcdsaToPem() (string, string)

Encodes Ecdsa private and public keys to PEM

func (*ECDSA) GenerateAndEncodeEcdsaKeys

func (e *ECDSA) GenerateAndEncodeEcdsaKeys() (string, string, error)

Generates ECDSA pair and encodes them to pem

func (ECDSA) SignEcdsaMessage

func (e ECDSA) SignEcdsaMessage(m interface{}) (*[]byte, error)

Signs a message (for example an struct) with the given RSA private key

Input:
  The message to sign, it must be serializable to a JSON
  the RSA Privatekey to sign the message
Output:
  The signature as byte array
  the error if there is any error getting the Hash or the signature

func (ECDSA) VerifyEcdsaMessage

func (e ECDSA) VerifyEcdsaMessage(m interface{}, signature []byte) bool

Verifies a message (for example an struct) signature using RSA public key

Input:
  The message to verify, it must be serializable to a JSON. Remember to remove the signature from message if you have included it in a field
  the RSA Public key to verify the message
  The signature as byte array
Output:
  true if the signature can be verified with the message or false elsewhere.

type EncryptResult

type EncryptResult struct {
	Salt                 []byte
	InitializationVector []byte
	Encrypted            []byte
}

func EncryptRaw

func EncryptRaw(passphrase, plaintext []byte, salt []byte, iv []byte) (*EncryptResult, error)

Encrypts the plain text using the passphrase and returns an EncryptResult struct containing the Salt, the Initialization Vector and the encrypted data. It is based on http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf, Section 8.2

Jump to

Keyboard shortcuts

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