Files
goget/internal/crypto/interface.go
T

108 lines
2.6 KiB
Go
Raw Normal View History

//go:build linux || freebsd
// +build linux freebsd
package crypto
import (
"io"
)
// Hasher is the interface for hashing functions
type Hasher interface {
// Name returns the name of the hash function
Name() string
// Hash calculates a hash from a reader
Hash(r io.Reader) (string, error)
// HashFile calculates a hash from a file
HashFile(path string) (string, error)
// Verify verifies a hash against the expected value
Verify(r io.Reader, expected string) (bool, error)
// VerifyFile verifies a file hash against the expected value
VerifyFile(path string, expected string) (bool, error)
}
// Encryptor is the interface for encryption
type Encryptor interface {
// Name returns the name of the cipher
Name() string
// Encrypt encrypts data
Encrypt(r io.Reader, w io.Writer, key []byte) error
// Decrypt decrypts data
Decrypt(r io.Reader, w io.Writer, key []byte) error
}
// Signer is the interface for digital signatures
type Signer interface {
// Name returns the name of the signature scheme
Name() string
// Sign creates a signature
Sign(data []byte, key []byte) ([]byte, error)
// Verify verifies a signature
Verify(data []byte, signature []byte, key []byte) (bool, error)
}
// KeyLoader is the interface for loading keys
type KeyLoader interface {
// LoadPrivateKey loads a private key
LoadPrivateKey(path string, passphrase []byte) ([]byte, error)
// LoadPublicKey loads a public key
LoadPublicKey(path string) ([]byte, error)
// GenerateKeyPair generates a key pair
GenerateKeyPair() (privateKey, publicKey []byte, err error)
}
// BaseHasher is a basic hasher implementation
type BaseHasher struct {
name string
}
// NewBaseHasher creates new base hasher
func NewBaseHasher(name string) *BaseHasher {
return &BaseHasher{name: name}
}
// Name returns the name of the hash function
func (bh *BaseHasher) Name() string {
return bh.name
}
// Hash calculates a hash from a reader
func (bh *BaseHasher) Hash(r io.Reader) (string, error) {
// Implementation in concrete hashers
return "", nil
}
// HashFile calculates a hash from a file
func (bh *BaseHasher) HashFile(path string) (string, error) {
// Implementation in concrete hashers
return "", nil
}
// Verify verifies a hash against the expected value
func (bh *BaseHasher) Verify(r io.Reader, expected string) (bool, error) {
actual, err := bh.Hash(r)
if err != nil {
return false, err
}
return actual == expected, nil
}
// VerifyFile verifies a file hash against the expected value
func (bh *BaseHasher) VerifyFile(path string, expected string) (bool, error) {
actual, err := bh.HashFile(path)
if err != nil {
return false, err
}
return actual == expected, nil
}