diff options
| author | 2026-01-27 19:05:00 +0100 | |
|---|---|---|
| committer | 2026-01-27 19:09:10 +0100 | |
| commit | 6002485582541df9dff3e2c782a014564e22ed07 (patch) | |
| tree | 0eeb6398044053859c2d80b7b8bc2e13d7fc01ac /repo.go | |
| parent | TODO: HashAlgorithm interface? compression agility? (diff) | |
| signature | No signature | |
hash: Use a hashAlgorithmDetails struct for single source of truth v0.1.2
hashAlgorithm's are assumed to be valid; methods on invalid
hashAlgorithms will panic from out-of-bounds read when it's not found in
hashAlgorithmTable and that's expected and intended.
Diffstat (limited to 'repo.go')
| -rw-r--r-- | repo.go | 23 |
1 files changed, 5 insertions, 18 deletions
@@ -63,23 +63,11 @@ func OpenRepository(path string) (*Repository, error) { algo = "sha1" } - var hashAlgo hashAlgorithm - switch algo { - case "sha1": - hashAlgo = hashAlgoSHA1 - case "sha256": - hashAlgo = hashAlgoSHA256 - default: + hashAlgo, ok := parseHashAlgorithm(algo) + if !ok { return nil, fmt.Errorf("furgit: unsupported hash algorithm %q", algo) } - if hashAlgo.size() == 0 { - return nil, fmt.Errorf("furgit: unsupported hash algorithm %q", algo) - } - if _, ok := hashFuncs[hashAlgo]; !ok { - return nil, fmt.Errorf("furgit: hash algorithm %q is not supported by the hash functions provided by this build", algo) - } - return &Repository{ rootPath: path, hashAlgo: hashAlgo, @@ -130,9 +118,9 @@ func (repo *Repository) ParseHash(s string) (Hash, error) { if len(s)%2 != 0 { return id, fmt.Errorf("furgit: invalid hash length %d, it has to be even at the very least", len(s)) } - expectedLen := repo.hashAlgo.size() * 2 + expectedLen := repo.hashAlgo.Size() * 2 if len(s) != expectedLen { - return id, fmt.Errorf("furgit: hash length mismatch: got %d chars, expected %d for hash size %d", len(s), expectedLen, repo.hashAlgo.size()) + return id, fmt.Errorf("furgit: hash length mismatch: got %d chars, expected %d for hash size %d", len(s), expectedLen, repo.hashAlgo.Size()) } data, err := hex.DecodeString(s) if err != nil { @@ -145,8 +133,7 @@ func (repo *Repository) ParseHash(s string) (Hash, error) { // computeRawHash computes a hash from raw data using the repository's hash algorithm. func (repo *Repository) computeRawHash(data []byte) Hash { - hashFunc := hashFuncs[repo.hashAlgo] - return hashFunc(data) + return repo.hashAlgo.Sum(data) } // verifyRawObject verifies a raw object against its expected hash. |
