crypto

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2025 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Database secret encryption using PEK without salt.
	// <remarks>Used until Windows Server 2000 Beta 2</remarks>
	SecretEncryptionType_DatabaseRC4 int = 0x10

	// Database secret encryption using PEK with salt.
	// <remarks>Used in Windows Server 2000 - Windows Server 2012 R2.</remarks>
	SecretEncryptionType_DatabaseRC4WithSalt int = 0x11

	// Replicated secret encryption using Session Key with salt.
	SecretEncryptionType_ReplicationRC4WithSalt int = 0x12

	// Database secret encryption using PEK and AES.
	// <remarks>Used since Windows Server 2016 TP4.</remarks>
	SecretEncryptionType_DatabaseAES int = 0x13
)

Variables

This section is empty.

Functions

func ExportPrivateKeyToPEM

func ExportPrivateKeyToPEM(rk *RSAKeyMaterial) string

ExportPEM exports the RSA key material to a PEM-encoded string.

Returns: - A string containing the PEM-encoded RSA key material.

Note: This function converts the RSA key material to the PKCS#1 RSAPrivateKey format and encodes it in PEM format. The PEM-encoded string can be used to store or transfer the RSA key material in a standardized format.

Types

type PrivateKeyEncryptionType

type PrivateKeyEncryptionType int
const (
	NONE PrivateKeyEncryptionType = iota
	PasswordRC4
	PasswordRC2CBC
)

func (PrivateKeyEncryptionType) String

func (pket PrivateKeyEncryptionType) String() string

type RSAKeyMaterial

type RSAKeyMaterial struct {
	KeyUsage    key.KeyUsage
	KeyStrength key.KeyStrength

	Exponent uint32
	Modulus  []byte
	Prime1   []byte
	Prime2   []byte
	KeySize  uint32

	// Internal
	RawBytes     []byte
	RawBytesSize uint32
}

RSAKeyMaterial represents the RSA key material structure.

See: https://docs.microsoft.com/en-us/archive/msdn-magazine/2007/july/applying-cryptography-using-the-cng-api-in-windows-vista https://docs.microsoft.com/en-us/archive/msdn-magazine/2007/july/images/cc163389.fig11.gif https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/ns-bcrypt-bcrypt_rsakey_blob

func (*RSAKeyMaterial) Describe

func (rk *RSAKeyMaterial) Describe(indent int)

Describe prints a detailed description of the RSAKeyMaterial instance.

Parameters: - indent: An integer representing the indentation level for the printed output.

Note: This function prints the Exponent, Modulus, Prime1, and Prime2 values of the RSAKeyMaterial instance. The output is formatted with the specified indentation level to improve readability. If Prime1 or Prime2 is not set, the function prints "None" for the respective value.

func (*RSAKeyMaterial) ExportPublicKeyToPEM

func (rk *RSAKeyMaterial) ExportPublicKeyToPEM(path string) error

ExportPublicKeyToPEM exports the RSA key material to a PEM-encoded string.

Returns: - A string containing the PEM-encoded RSA key material.

Note: This function converts the RSA key material to the PKCS#1 RSAPublicKey format and encodes it in PEM format. The PEM-encoded string can be used to store or transfer the RSA key material in a standardized format.

func (*RSAKeyMaterial) FromBytes

func (rk *RSAKeyMaterial) FromBytes(value []byte) error

Parse parses the provided byte slice into the RSAKeyMaterial structure.

Parameters: - value: A byte slice containing the raw RSA key material to be parsed.

Returns: - An error if the parsing fails, otherwise nil.

Note: The function expects the byte slice to follow the RSA key blob format, starting with the "RSA1" blob type identifier. It extracts the key size, exponent size, modulus size, prime1 size, and prime2 size from the header, and then parses the corresponding values from the body of the byte slice. The parsed values are stored in the RSAKeyMaterial structure.

The function performs the following steps: 1. Sets the RawBytes and RawBytesSize fields to the provided byte slice and its length, respectively. 2. Checks if the blob type is "RSA1". If not, it returns an error. 3. Extracts the key size, exponent size, modulus size, prime1 size, and prime2 size from the header. 4. Parses the exponent, modulus, prime1, and prime2 values from the body of the byte slice based on the extracted sizes. 5. Stores the parsed values in the corresponding fields of the RSAKeyMaterial structure.

func (*RSAKeyMaterial) String

func (rk *RSAKeyMaterial) String() string

String returns a string representation of the RSAKeyMaterial structure.

Returns: - A string representing the RSAKeyMaterial structure.

func (*RSAKeyMaterial) ToBytes

func (rk *RSAKeyMaterial) ToBytes() []byte

ToBytes returns the raw bytes of the RSAKeyMaterial structure.

Returns: - A byte slice representing the raw bytes of the RSAKeyMaterial structure.

type SecretEncryptionType

type SecretEncryptionType struct {
	Value int

	// Internal
	RawBytes     []byte
	RawBytesSize uint32
}

func (*SecretEncryptionType) FromBytes

func (set *SecretEncryptionType) FromBytes(value []byte)

FromBytes parses the SecretEncryptionType from a byte array.

Parameters: - value: A byte array containing the SecretEncryptionType to be parsed.

func (*SecretEncryptionType) String

func (set *SecretEncryptionType) String() string

String returns the string representation of the SecretEncryptionType.

func (*SecretEncryptionType) ToBytes

func (set *SecretEncryptionType) ToBytes() []byte

ToBytes returns the raw bytes of the SecretEncryptionType.

Returns: - A byte array containing the raw bytes of the SecretEncryptionType.

type X509Certificate

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

X509Certificate represents an X.509 certificate along with its associated RSA private key and public key material.

Fields: - key: A pointer to an rsa.PrivateKey object representing the RSA private key associated with the certificate. - certificate: A pointer to an x509.Certificate object representing the X.509 certificate. - publicKey: An RSAKeyMaterial object representing the public key material of the certificate.

Methods: - NewX509Certificate: Creates a new X.509 certificate with the specified subject, key size, and validity period. - ExportPFX: Exports the certificate and private key to a PFX file with the specified password.

Note: The X509Certificate struct is used to manage X.509 certificates, including the generation of new certificates and the export of certificates and private keys to PFX files. The struct includes fields for the RSA private key, X.509 certificate, and public key material. The NewX509Certificate method is used to create a new certificate, and the ExportPFX method is used to export the certificate and private key to a PFX file.

func NewX509Certificate

func NewX509Certificate(subject string, keySize int, notBefore, notAfter time.Time) (*X509Certificate, error)

NewX509Certificate creates a new X.509 certificate with the specified subject, key size, and validity period.

Parameters: - subject: A string representing the common name (CN) of the certificate subject. - keySize: An integer specifying the size of the RSA key to be generated (e.g., 2048, 4096). - notBefore: A time.Time object representing the start of the certificate's validity period. - notAfter: A time.Time object representing the end of the certificate's validity period.

Returns: - A pointer to an X509Certificate object containing the generated certificate and associated RSA private key. - An error if the certificate generation fails.

Note: The function performs the following steps: 1. Generates a new RSA private key with the specified key size. 2. Creates a serial number for the certificate. 3. Constructs a certificate template with the specified subject, validity period, key usage, and extended key usage. 4. Creates a self-signed X.509 certificate using the generated RSA private key and certificate template. 5. Parses the generated certificate and returns an X509Certificate object containing the certificate and private key.

Example usage: cert, err := NewX509Certificate("example.com", 2048, time.Now(), time.Now().AddDate(1, 0, 0))

if err != nil {
    fmt.Printf("Error creating X509Certificate: %s\n", err)
}

func (*X509Certificate) ExportPFX

func (x *X509Certificate) ExportPFX(pathToFile, password string) error

ExportPFX exports the certificate and private key to a PFX file with the specified password.

Parameters: - pathToFile: A string representing the path to the file where the PFX will be exported. - password: A string representing the password for the PFX file.

Returns: - An error if the export fails, otherwise nil.

func (*X509Certificate) ExportRSAPrivateKeyPEM

func (x *X509Certificate) ExportRSAPrivateKeyPEM(pathToFile string) error

ExportRSAPrivateKeyPEM exports the private key to a PEM file.

Parameters: - pathToFile: A string representing the path to the file where the private key will be exported.

Returns: - An error if the export fails, otherwise nil.

func (*X509Certificate) ExportRSAPublicKey

func (x *X509Certificate) ExportRSAPublicKey() RSAKeyMaterial

ExportRSAPublicKey returns the public key material of the certificate.

Returns: - An RSAKeyMaterial object representing the public key material of the certificate.

func (*X509Certificate) ExportRSAPublicKeyBCrypt

func (x *X509Certificate) ExportRSAPublicKeyBCrypt() ([]byte, error)

ExportRSAPublicKeyBCrypt exports the public key to a BCrypt file.

Parameters: - pathToFile: A string representing the path to the file where the public key will be exported.

Returns: - An error if the export fails, otherwise nil.

func (*X509Certificate) ExportRSAPublicKeyDER

func (x *X509Certificate) ExportRSAPublicKeyDER() ([]byte, error)

ExportRSAPublicKeyDER exports the public key to a DER file.

Parameters: - pathToFile: A string representing the path to the file where the public key will be exported.

Returns: - An error if the export fails, otherwise nil.

func (*X509Certificate) ExportRSAPublicKeyPEM

func (x *X509Certificate) ExportRSAPublicKeyPEM(pathToFile string) error

ExportRSAPublicKeyPEM exports the public key to a PEM file.

Parameters: - pathToFile: A string representing the path to the file where the public key will be exported.

Returns: - An error if the export fails, otherwise nil.

func (*X509Certificate) GetCertificate

func (x *X509Certificate) GetCertificate() *x509.Certificate

GetCertificate returns the certificate of the certificate.

Returns: - A pointer to an x509.Certificate object representing the certificate of the certificate.

func (*X509Certificate) GetRSAKeyMaterial

func (x *X509Certificate) GetRSAKeyMaterial() RSAKeyMaterial

GetRSAKeyMaterial returns the RSA key material of the certificate.

Returns: - An RSAKeyMaterial object representing the RSA key material of the certificate.

func (*X509Certificate) GetRSAPrivateKey

func (x *X509Certificate) GetRSAPrivateKey() *rsa.PrivateKey

GetRSAPrivateKey returns the private key of the certificate.

Returns: - A pointer to an rsa.PrivateKey object representing the private key of the certificate.

func (*X509Certificate) GetRSAPublicKey

func (x *X509Certificate) GetRSAPublicKey() *rsa.PublicKey

GetRSAPublicKey returns the public key of the certificate.

Returns: - A pointer to an rsa.PublicKey object representing the public key of the certificate.

Jump to

Keyboard shortcuts

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