crypto

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyMustBePEMEncoded = errors.New(errors.ErrorGeneratingRsa, "invalid Key: Key must be a PEM encoded PKCS1 or PKCS8 key")
	ErrNotRSAPrivateKey    = errors.New(errors.ErrorGeneratingRsa, "key is not a valid RSA private key")
	ErrNotRSAPublicKey     = errors.New(errors.ErrorGeneratingRsa, "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 RSACreatePairToPemFiles

func RSACreatePairToPemFiles() (*string, *string, 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