Documentation
¶
Index ¶
- func HashingAlg(algo HashingAlgo) crypto.Hash
- func HashingInstance(algo HashingAlgo) func() hash.Hash
- func HashingSize(algo HashingAlgo) int
- func ParseCertificate(cert []byte, isPEM bool) (*x509.Certificate, error)
- func ParseCertificateRequest(csr []byte, isPEM bool) (*x509.CertificateRequest, error)
- func ParsePublicKey(key []byte, isPEM bool) (*rsa.PublicKey, error)
- type Cryptography
- type Hasher
- type HashingAlgo
- type RSAKey
- func (k *RSAKey) IssueCertificateBytes(subject pkix.Name, algo x509.SignatureAlgorithm, options *x509.Certificate) ([]byte, error)
- func (k *RSAKey) IssueCertificatePEM(subject pkix.Name, algo x509.SignatureAlgorithm, options *x509.Certificate) ([]byte, error)
- func (k *RSAKey) PrivateKey() *rsa.PrivateKey
- func (k *RSAKey) PrivateKeyBytes(usePKCS8 bool) ([]byte, error)
- func (k *RSAKey) PrivateKeyPEM(usePKCS8 bool) ([]byte, error)
- func (k *RSAKey) PublicKey() *rsa.PublicKey
- func (k *RSAKey) PublicKeyBytes(usePKCS8 bool) ([]byte, error)
- func (k *RSAKey) PublicKeyPEM(usePKCS8 bool) ([]byte, error)
- type RSALength
- type SimpleKey
- type SimpleLength
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HashingAlg ¶
func HashingAlg(algo HashingAlgo) crypto.Hash
HashingAlg maps the hashing algorithm to its corresponding crypto.Hash value. Returns 0 if the algorithm is not supported.
func HashingInstance ¶
func HashingInstance(algo HashingAlgo) func() hash.Hash
HashingInstance returns a new hash.Hash instance for the given algorithm. Returns nil if the algorithm is not supported.
func HashingSize ¶
func HashingSize(algo HashingAlgo) int
HashingSize returns the size of the hash output for the given algorithm in bytes. Returns 0 if the algorithm is not supported.
func ParseCertificate ¶
func ParseCertificate(cert []byte, isPEM bool) (*x509.Certificate, error)
ParseCertificate parses an x509 certificate.
func ParseCertificateRequest ¶
func ParseCertificateRequest(csr []byte, isPEM bool) (*x509.CertificateRequest, error)
ParseCertificateRequest parses an x509 certificate request (CSR).
Types ¶
type Cryptography ¶
type Cryptography interface { // Sign generates a cryptographic signature for the given data. // Returns the signature as a byte slice or an error if signing fails. Sign(data []byte) ([]byte, error) // ValidateSignature checks if the provided signature is valid for the given data. // Returns true if the signature is valid, false otherwise, along with any error encountered. ValidateSignature(data []byte, signature []byte) (bool, error) // Encrypt encrypts the given data and returns the encrypted result as a byte slice. // Returns an error if encryption fails. Encrypt(data []byte) ([]byte, error) // Decrypt decrypts the given encrypted data and returns the original data as a byte slice. // Returns an error if decryption fails. Decrypt(data []byte) ([]byte, error) // EncryptBase64 encrypts the given data and encodes the result as a base64 string. // Returns the base64-encoded encrypted string or an error if encryption fails. EncryptBase64(data []byte) (string, error) // DecryptBase64 decodes the given base64-encoded string and decrypts it. // Returns the original data as a byte slice or an error if decryption fails. DecryptBase64(encrypted string) ([]byte, error) }
Cryptography provides signing, validating, encrypting, and decrypting data. It provides support for both raw byte operations and base64-encoded string operations.
func NewAsymmetric ¶
func NewAsymmetric(key RSAKey, signer HashingAlgo) Cryptography
NewAsymmetric creates a new asymmetric encryption driver with a private key.
func NewAsymmetricClient ¶
func NewAsymmetricClient(public *rsa.PublicKey, signer HashingAlgo) Cryptography
NewAsymmetricClient creates a new asymmetric encryption driver with a public key.
func NewSymmetric ¶
func NewSymmetric(key SimpleKey, signer HashingAlgo) Cryptography
NewSymmetric creates a new symmetric encryption driver.
type Hasher ¶
type Hasher interface { // Hash generates a hash from the provided data. // Returns the hashed data or an error if the hashing process fails. Hash(data []byte) ([]byte, error) // Validate compares a hashed value with its possible plaintext equivalent. // Returns true if the hash matches the data, otherwise false. // An error is returned if the validation process fails. Validate(hash, data []byte) (bool, error) }
Hasher generate and validate hash from data. It is recommended to use secure hashing algorithms like Argon2 or bcrypt for sensitive use cases such as password hashing.
func HMacHasher ¶
func HMacHasher(key []byte, algo HashingAlgo) Hasher
HMacHasher creates a new HMAC hasher instance (recommended for message signing).
func NewArgon2Hasher ¶
func NewArgon2Hasher( saltLength SimpleLength, keyLength uint32, memory uint32, iterations uint32, parallelism uint8, ) Hasher
NewArgon2Hasher creates a new Argon2 hasher with the specified parameters (recommended for password). If a parameter is 0, a default value is used.
func NewBcryptHasher ¶
NewBcryptHasher creates a new bcrypt hasher (alternative for password). If a parameter is 0, a default value is used.
type HashingAlgo ¶
type HashingAlgo string
HashingAlgo represents the hashing algorithm.
const ( MD5 HashingAlgo = "MD5" // MD5 algorithm (not recommended for security) SHA1 HashingAlgo = "SHA1" // SHA1 algorithm (not recommended for security) SHA224 HashingAlgo = "SHA224" // SHA2 224 algorithm SHA256 HashingAlgo = "SHA256" // SHA2 256 algorithm (recommended) SHA384 HashingAlgo = "SHA384" // SHA2 384 algorithm SHA512 HashingAlgo = "SHA512" // SHA2 512 algorithm SHA3224 HashingAlgo = "SHA3224" // SHA3 224 algorithm SHA3256 HashingAlgo = "SHA3256" // SHA3 256 algorithm SHA3384 HashingAlgo = "SHA3384" // SHA3 384 algorithm SHA3512 HashingAlgo = "SHA3512" // SHA3 512 algorithm )
Supported hashing algorithms.
type RSAKey ¶
type RSAKey struct {
// contains filtered or unexported fields
}
RSAKey wraps an RSA private key and provides utility methods.
func ParsePrivateKey ¶
ParsePrivateKey parses an RSA private key from PKCS #1 or PKCS #8 format.
func (*RSAKey) IssueCertificateBytes ¶
func (k *RSAKey) IssueCertificateBytes(subject pkix.Name, algo x509.SignatureAlgorithm, options *x509.Certificate) ([]byte, error)
IssueCertificateBytes generates a self-signed certificate in DER format.
func (*RSAKey) IssueCertificatePEM ¶
func (k *RSAKey) IssueCertificatePEM(subject pkix.Name, algo x509.SignatureAlgorithm, options *x509.Certificate) ([]byte, error)
IssueCertificatePEM generates a self-signed certificate in PEM format.
func (*RSAKey) PrivateKey ¶
func (k *RSAKey) PrivateKey() *rsa.PrivateKey
PrivateKey returns the RSA private key.
func (*RSAKey) PrivateKeyBytes ¶
PrivateKeyBytes returns the private key in PKCS#1 or PKCS#8 format. PKCS #1 is not recommended for security as it's considered weak and can potentially be broken by modern computational capabilities.
func (*RSAKey) PrivateKeyPEM ¶
PrivateKeyPEM returns the private key in PEM-encoded format (PKCS#1 or PKCS#8). PKCS #1 is not recommended for security as it's considered weak and can potentially be broken by modern computational capabilities.
func (*RSAKey) PublicKeyBytes ¶
PublicKeyBytes returns the public key in PKIX or PKCS#1 format. PKCS #1 is not recommended for security as it's considered weak and can potentially be broken by modern computational capabilities.
type SimpleKey ¶
type SimpleKey []byte
SimpleKey represents a cryptographic key as a byte slice.
func NewSimpleKey ¶
func NewSimpleKey(length SimpleLength) (SimpleKey, error)
NewSimpleKey generates a new random key of the specified length. Returns an error if the random number generation fails.
type SimpleLength ¶
type SimpleLength int
SimpleLength defines the length of the key in bytes.
const ( Simple16 SimpleLength = 16 // 128-bit key Simple24 SimpleLength = 24 // 192-bit key Simple32 SimpleLength = 32 // 256-bit key )
Predefined key lengths for convenience.