Documentation
¶
Overview ¶
Package cryptoutil provides cryptographic utilities for hashing, encryption, and signature operations
Index ¶
- Variables
- func Bytes2Hex(d []byte) string
- func CalculateFileChecksum(filePath string, algorithm HashAlgorithm) (string, error)
- func FindSignatureFile(filePath string) (string, error)
- func IsSignatureFile(path string) bool
- func VerifyChecksumFile(filePath, checksumFilePath string, algorithm HashAlgorithm) (bool, error)
- func VerifyFileChecksum(filePath, expectedChecksum string, algorithm HashAlgorithm) (bool, error)
- func VerifySignature(filePath, signaturePath string) (bool, error)
- type GPGSignatureVerifier
- type HashAlgorithm
- type HashWriter
- type Hasher
- type MacOSSignatureVerifier
- type SignatureVerifier
- type WindowsSignatureVerifier
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidHasher = errors.New("invalid hasher implementation")
)
Custom errors
Functions ¶
func CalculateFileChecksum ¶
func CalculateFileChecksum(filePath string, algorithm HashAlgorithm) (string, error)
CalculateFileChecksum calculates a file's checksum using the specified algorithm
func FindSignatureFile ¶
FindSignatureFile attempts to find a signature file for the given file
func IsSignatureFile ¶
IsSignatureFile checks if a file is likely a signature file based on its extension
func VerifyChecksumFile ¶
func VerifyChecksumFile(filePath, checksumFilePath string, algorithm HashAlgorithm) (bool, error)
VerifyChecksumFile verifies a file against a checksum file The checksum file should contain the hash as the first field on each line
func VerifyFileChecksum ¶
func VerifyFileChecksum(filePath, expectedChecksum string, algorithm HashAlgorithm) (bool, error)
VerifyFileChecksum verifies a file's checksum against an expected value
func VerifySignature ¶
VerifySignature is a convenience function that verifies a file's signature using the appropriate verifier for the current OS
Types ¶
type GPGSignatureVerifier ¶
type GPGSignatureVerifier struct{}
GPGSignatureVerifier verifies signatures using GnuPG
func NewGPGSignatureVerifier ¶
func NewGPGSignatureVerifier() *GPGSignatureVerifier
NewGPGSignatureVerifier creates a new GPG signature verifier
func (*GPGSignatureVerifier) VerifyDetachedSignature ¶
func (v *GPGSignatureVerifier) VerifyDetachedSignature(filePath, signaturePath string) (bool, error)
VerifyDetachedSignature verifies a detached signature using gpg
func (*GPGSignatureVerifier) VerifyFile ¶
func (v *GPGSignatureVerifier) VerifyFile(filePath, _ string) (bool, error)
VerifyFile verifies a file's signature using gpg
type HashAlgorithm ¶
type HashAlgorithm string
HashAlgorithm represents supported hash algorithms
const ( // MD5 algorithm (not recommended for security-critical applications) MD5 HashAlgorithm = "md5" // SHA1 algorithm (not recommended for security-critical applications) SHA1 HashAlgorithm = "sha1" // SHA256 algorithm SHA256 HashAlgorithm = "sha256" // SHA512 algorithm SHA512 HashAlgorithm = "sha512" )
func ParseHashWithAlgorithm ¶
func ParseHashWithAlgorithm(hashStr string) (string, HashAlgorithm)
ParseHashWithAlgorithm parses a hash string that might include the algorithm as a prefix Example formats: "sha256:1234abcd..." or "1234abcd..."
type HashWriter ¶
type HashWriter struct {
// contains filtered or unexported fields
}
HashWriter implements io.Writer and provides methods to access the underlying hash
func NewHashWriter ¶
func NewHashWriter(algorithm HashAlgorithm) (*HashWriter, error)
NewHashWriter creates a new HashWriter with the given hash algorithm
func (*HashWriter) BlockSize ¶
func (hw *HashWriter) BlockSize() int
BlockSize returns the hash's underlying block size in bytes
func (*HashWriter) Size ¶
func (hw *HashWriter) Size() int
Size returns the hash's output size in bytes
func (*HashWriter) Sum ¶
func (hw *HashWriter) Sum(b []byte) []byte
Sum returns the current hash value
func (*HashWriter) SumHex ¶
func (hw *HashWriter) SumHex() string
SumHex returns the current hash value as a hex-encoded string
type Hasher ¶
type Hasher interface { // Hash hashes the provided data Hash(data []byte) (string, error) // HashFile hashes the content of a file HashFile(path string) (string, error) // HashReader hashes data from a reader HashReader(reader io.Reader) (string, error) // NewHashWriter creates a writer for streaming hash calculation NewHashWriter() (io.Writer, error) // Verify checks if the provided hash matches the calculated hash for the data Verify(data []byte, expectedHash string) (bool, error) // VerifyFile checks if the provided hash matches the calculated hash for the file VerifyFile(path string, expectedHash string) (bool, error) }
Hasher provides an interface for hashing operations
func NewHasher ¶
func NewHasher(algorithm HashAlgorithm) (Hasher, error)
NewHasher creates a new Hasher for the specified algorithm
type MacOSSignatureVerifier ¶
type MacOSSignatureVerifier struct{}
MacOSSignatureVerifier verifies signatures using macOS codesign
func NewMacOSSignatureVerifier ¶
func NewMacOSSignatureVerifier() *MacOSSignatureVerifier
NewMacOSSignatureVerifier creates a new macOS signature verifier
func (*MacOSSignatureVerifier) VerifyDetachedSignature ¶
func (v *MacOSSignatureVerifier) VerifyDetachedSignature(filePath, signaturePath string) (bool, error)
VerifyDetachedSignature verifies a detached signature (not applicable for codesign)
func (*MacOSSignatureVerifier) VerifyFile ¶
func (v *MacOSSignatureVerifier) VerifyFile(filePath, _ string) (bool, error)
VerifyFile verifies a file's signature using codesign
type SignatureVerifier ¶
type SignatureVerifier interface { // VerifyFile checks if a file's signature is valid VerifyFile(filePath, signaturePath string) (bool, error) // VerifyDetachedSignature verifies a detached signature for a file VerifyDetachedSignature(filePath, signaturePath string) (bool, error) }
SignatureVerifier provides an interface for verifying digital signatures
func GetSignatureVerifier ¶
func GetSignatureVerifier() SignatureVerifier
GetSignatureVerifier returns an appropriate signature verifier based on the current OS
type WindowsSignatureVerifier ¶
type WindowsSignatureVerifier struct{}
WindowsSignatureVerifier verifies signatures using Windows signtool
func NewWindowsSignatureVerifier ¶
func NewWindowsSignatureVerifier() *WindowsSignatureVerifier
NewWindowsSignatureVerifier creates a new Windows signature verifier
func (*WindowsSignatureVerifier) VerifyDetachedSignature ¶
func (v *WindowsSignatureVerifier) VerifyDetachedSignature(filePath, signaturePath string) (bool, error)
VerifyDetachedSignature verifies a detached signature (not applicable for signtool)
func (*WindowsSignatureVerifier) VerifyFile ¶
func (v *WindowsSignatureVerifier) VerifyFile(filePath, _ string) (bool, error)
VerifyFile verifies a file's signature using signtool