cryptoutil

package
v0.0.0-...-fd57926 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package cryptoutil provides cryptographic utilities for hashing, encryption, and signature operations

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHasher = errors.New("invalid hasher implementation")
)

Custom errors

Functions

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex encodes a byte slice to hex string

func CalculateFileChecksum

func CalculateFileChecksum(filePath string, algorithm HashAlgorithm) (string, error)

CalculateFileChecksum calculates a file's checksum using the specified algorithm

func FindSignatureFile

func FindSignatureFile(filePath string) (string, error)

FindSignatureFile attempts to find a signature file for the given file

func IsSignatureFile

func IsSignatureFile(path string) bool

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

func VerifySignature(filePath, signaturePath string) (bool, error)

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) Reset

func (hw *HashWriter) Reset()

Reset resets the hash state

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

func (*HashWriter) Write

func (hw *HashWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

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

Jump to

Keyboard shortcuts

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