crypto

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 28 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Certificate

type Certificate struct {
	Cert string
	Key  string
}

type CryptoRegistry

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

func NewRegistry

func NewRegistry() *CryptoRegistry

NewRegistry creates a new instance of CryptoRegistry with an embedded Handler.

func (*CryptoRegistry) Bcrypt

func (ch *CryptoRegistry) Bcrypt(input string) (string, error)

Bcrypt generates a bcrypt hash from the given input string.

input - the string to be hashed. Returns the bcrypt hash as a string.

Example:

{{ "Hello World" | bcrypt }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"

func (*CryptoRegistry) BuildCustomCertificate

func (ch *CryptoRegistry) BuildCustomCertificate(b64cert string, b64key string) (Certificate, error)

BuildCustomCertificate builds a custom certificate from a base64 encoded certificate and private key.

b64cert - the base64 encoded certificate. b64key - the base64 encoded private key. Returns a certificate and an error.

Example:

{{ buildCustomCertificate "b64cert" "b64key" }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) DecryptAES

func (ch *CryptoRegistry) DecryptAES(password string, crypt64 string) (string, error)

DecryptAES decrypts the given base64-encoded AES-encrypted string using the provided password.

Parameters:

  • password: the password to use for decryption
  • crypt64: the base64-encoded AES-encrypted string to be decrypted

Returns:

  • string: the decrypted text
  • error: an error if any occurred during the decryption process

Example:

{{ decryptAES "password" "b64encrypted" }} // Output: "plaintext"

func (*CryptoRegistry) DerivePassword

func (ch *CryptoRegistry) DerivePassword(counter uint32, passwordType, password, user, site string) (string, error)

DerivePassword derives a password based on the given counter, password type, password, user, and site.

counter - the counter value used in the password derivation process. passwordType - the type of password to derive. password - the password used in the derivation process. user - the user string used in the derivation process. site - the site string used in the derivation process. Returns the derived password as a string.

Example:

{{ derivePassword 0 "bcrypt" "password" "user" "site" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"

func (*CryptoRegistry) EncryptAES

func (ch *CryptoRegistry) EncryptAES(password string, plaintext string) (string, error)

EncryptAES encrypts a plaintext string using AES encryption with a given password.

Parameters:

  • password: the password to use for encryption
  • plaintext: the text to be encrypted

Returns:

  • string: the encrypted text as a base64-encoded string
  • error: an error if any occurred during the encryption process

Example:

{{ encryptAES "password" "plaintext" }} // Output: "b64encrypted"

func (*CryptoRegistry) GenerateCertificateAuthority

func (ch *CryptoRegistry) GenerateCertificateAuthority(
	cn string,
	daysValid int,
) (Certificate, error)

GenerateCertificateAuthority generates a certificate authority using the provided common name and validity period.

Parameters:

  • cn: the common name for the certificate authority
  • daysValid: the number of days the certificate authority is valid for

Returns:

  • Certificate: the generated certificate authority
  • error: an error if any occurred during the generation process

Example:

{{ generateCertificateAuthority "example.com" 365 }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) GenerateCertificateAuthorityWithPEMKey

func (ch *CryptoRegistry) GenerateCertificateAuthorityWithPEMKey(
	cn string,
	daysValid int,
	privPEM string,
) (Certificate, error)

GenerateCertificateAuthorityWithPEMKey generates a certificate authority using the provided common name, validity period, and private key in PEM format.

Parameters:

  • cn: the common name for the certificate authority
  • daysValid: the number of days the certificate authority is valid for
  • privPEM: the private key in PEM format

Returns:

  • Certificate: the generated certificate authority
  • error: an error if any occurred during the generation process

Example:

{{ generateCertificateAuthorityWithPEMKey "example.com" 365 "privPEM" }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) GeneratePrivateKey

func (ch *CryptoRegistry) GeneratePrivateKey(typ string) (string, error)

GeneratePrivateKey generates a private key of the specified type.

typ - the type of private key to generate (e.g., "rsa", "dsa", "ecdsa", "ed25519"). Returns the generated private key as a string.

Example:

{{ generatePrivateKey "rsa" }} // Output: "-----BEGIN RSA PRIVATE KEY-----"

func (*CryptoRegistry) GenerateSelfSignedCertificate

func (ch *CryptoRegistry) GenerateSelfSignedCertificate(
	cn string,
	ips []any,
	alternateDNS []any,
	daysValid int,
) (Certificate, error)

GenerateSelfSignedCertificate generates a new, self-signed x509 certificate using a 2048-bit RSA private key.

Parameters:

  • cn: the common name for the certificate
  • ips: a list of IP addresses
  • alternateDNS: a list of alternate DNS names
  • daysValid: the number of days the certificate is valid for

Returns:

  • Certificate: the generated certificate
  • error: an error if any occurred during the generation process

Example:

{{ generateSelfSignedCertificate "example.com" ["127.0.0.1"] ["localhost"] 365 }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) GenerateSelfSignedCertificateWithPEMKey

func (ch *CryptoRegistry) GenerateSelfSignedCertificateWithPEMKey(
	cn string,
	ips []any,
	alternateDNS []any,
	daysValid int,
	privPEM string,
) (Certificate, error)

GenerateSelfSignedCertificateWithPEMKey generates a new, self-signed x509 certificate using a given private key in PEM format.

Parameters:

  • cn: the common name for the certificate
  • ips: a list of IP addresses
  • alternateDNS: a list of alternate DNS names
  • daysValid: the number of days the certificate is valid for
  • privPEM: the private key in PEM format

Returns:

  • Certificate: the generated certificate
  • error: an error if any occurred during the generation process

Example:

{{ generateSelfSignedCertificateWithPEMKey "example.com" ["127.0.0.1"] ["localhost"] 365 "privPEM" }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) GenerateSignedCertificate

func (ch *CryptoRegistry) GenerateSignedCertificate(
	cn string,
	ips []any,
	alternateDNS []any,
	daysValid int,
	ca Certificate,
) (Certificate, error)

GenerateSignedCertificate generates a new, signed x509 certificate using a given CA certificate.

Parameters:

  • cn: the common name for the certificate
  • ips: a list of IP addresses
  • alternateDNS: a list of alternate DNS names
  • daysValid: the number of days the certificate is valid for
  • ca: the CA certificate to sign with

Returns:

  • Certificate: the generated certificate
  • error: an error if any occurred during the generation process

Example:

{{ generateSignedCertificate "example.com" ["127.0.0.1"] ["localhost"] 365 ca }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) GenerateSignedCertificateWithPEMKey

func (ch *CryptoRegistry) GenerateSignedCertificateWithPEMKey(
	cn string,
	ips []any,
	alternateDNS []any,
	daysValid int,
	ca Certificate,
	privPEM string,
) (Certificate, error)

GenerateSignedCertificateWithPEMKey generates a new, signed x509 certificate using a given CA certificate and a private key in PEM format.

Parameters:

  • cn: the common name for the certificate
  • ips: a list of IP addresses
  • alternateDNS: a list of alternate DNS names
  • daysValid: the number of days the certificate is valid for
  • ca: the CA certificate to sign with
  • privPEM: the private key in PEM format

Returns:

  • Certificate: the generated certificate
  • error: an error if any occurred during the generation process

Example:

{{ generateSignedCertificateWithPEMKey "example.com" ["127.0.0.1"] ["localhost"] 365 ca "privPEM" }} // Output: {"Cert":"b64cert","Key":"b64key"}

func (*CryptoRegistry) Htpasswd

func (ch *CryptoRegistry) Htpasswd(username string, password string) (string, error)

Htpasswd generates an Htpasswd hash from the given username and password strings.

username - the username string for the Htpasswd hash. password - the password string for the Htpasswd hash. Returns the generated Htpasswd hash as a string.

Example:

{{ htpasswd "username" "password" }} // Output: "$2a$12$C1qL8XVjIuGKzQXwC6g6tO"

func (*CryptoRegistry) LinkHandler

func (ch *CryptoRegistry) LinkHandler(fh sprout.Handler) error

func (*CryptoRegistry) RegisterFunctions

func (ch *CryptoRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error

RegisterFunctions adds all crypto-related functions to the provided registry.

func (*CryptoRegistry) Uid

func (ch *CryptoRegistry) Uid() string

Uid returns the unique identifier of the crypto handler.

type DSAKeyFormat

type DSAKeyFormat struct {
	Version       int
	P, Q, G, Y, X *big.Int
}

DSAKeyFormat stores the format for DSA keys. Used by pemBlockForKey

Jump to

Keyboard shortcuts

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