hasher

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: Apache-2.0 Imports: 14 Imported by: 3

README

github.com/carabiner-dev/hasher

A simple go module to hash data focused on simplicity, performance and ease of use.

Install the Module

To install in your project simply use go get:

go get github.com/carabiner-dev/hasher

Examples

The hasher object can hash lists of io.Reader or a list of file paths:

package main

import (
    "github.com/carabiner-dev/hasher"
    intoto "github.com/in-toto/attestation/go/v1"
)

// Simple program to compute checksums of files
func main() {
    h := hasher.New()

    // Configure the algorithms hasher will use (optional):
    h.Options.Algorithms = []intoto.HashAlgorithm{
		intoto.AlgorithmSHA256, intoto.AlgorithmSHA512,
	}

    // Configure how many files to hash at once:
    h.Options.MaxParallel = 2

    // Run the hasher:
    hashes, err := h.HashFiles([]string{
        "hello.txt", "README.md",
    })
    if err != nil {
        os.Exit(1)
    }

    // Print the results:
    for path, hs := range hashes {
        fmt.Println(path + ":")
        for algo, value := range hs {
            fmt.Printf("  %s:%s\n", algo, value)
        }
    }
}

The resulting types can be natively converted to in-toto Resource Descriptors to easily use them when building attestations:

  // Hash a bunch of files:
  hashes, err := h.HashFiles([]string{"hello.txt", "README.md",})

  // Convert to a slice of intoto.ResourceDescriptor:
  descriptors = hashes.ToResourceDescriptors()

Implementation

The hasher implementation runs the hashing processes in parallel. While the sums for each algorithm are computed serially, hasher handles four input streams at the same time by default.

The list of supported hashes is imported from the hashing algorithms recognized in the in-toto project.

Note that the list of paths to hashed is artificially capped at 1024 to avoid ddos'ing the hasher.

License

This module is open source, licensed under the Apache 2.0 license

Documentation

Index

Constants

This section is empty.

Variables

HasherFactory is a preconfigured hasher map with all the known algorithm types

Functions

This section is empty.

Types

type FileHashSet

type FileHashSet map[string]HashSet

FileHashSet captures a set of hashes of the same artifact indexed by its path.

func (*FileHashSet) ToResourceDescriptors

func (fhs *FileHashSet) ToResourceDescriptors() []*intoto.ResourceDescriptor

type HashSet

type HashSet map[intoto.HashAlgorithm]string

HashSet captures a set of hashes of the same artifact.

func NewHashSet added in v0.2.1

func NewHashSet[K ~string, T ~string](data map[K]T) *HashSet

NewHashSet returns a new hashset from any map of string-like things

func (*HashSet) ToResourceDescriptor

func (hs *HashSet) ToResourceDescriptor() *intoto.ResourceDescriptor

type HashSetList

type HashSetList []HashSet

HashSetList is an array of HashSets.

func (*HashSetList) ToResourceDescriptors

func (hsl *HashSetList) ToResourceDescriptors() []*intoto.ResourceDescriptor

type Hasher

type Hasher struct {
	Options Options
}

Hasher is an object that has methods to hash data.

func New

func New() *Hasher

New returns a new Hasher configured with the default options

func (*Hasher) HashFiles

func (h *Hasher) HashFiles(paths []string) (*FileHashSet, error)

HashFiles gets a list of paths and returns the file hashes indexed by path name. The results are guaranteed to be in the same order but any repeated paths will be hashed more than once and included only once in the returned structure.

func (*Hasher) HashReaders

func (h *Hasher) HashReaders(readers []io.Reader) (*HashSetList, error)

HashFiles takes a list of files and returns the hashes for them

func (*Hasher) VerifyReader added in v0.2.0

func (h *Hasher) VerifyReader(r io.Reader, set *HashSet) (bool, error)

VerifyReader compares a set of hashes against a reader data. If all match then it returns true. If a hash mismatches, it returns false.

type HasherFactoryMap

type HasherFactoryMap map[intoto.HashAlgorithm]func() hash.Hash

HasherFactoryMap is a map that can create hash.Hash objects for all the recognized algorithm types.

func (*HasherFactoryMap) GetHasher

func (hf *HasherFactoryMap) GetHasher(algo intoto.HashAlgorithm) hash.Hash

type OptFn added in v0.2.0

type OptFn func(*Options) error

func WithAlgorithms added in v0.2.0

func WithAlgorithms[T ~string](algos []T) OptFn

type Options

type Options struct {
	Algorithms  []intoto.HashAlgorithm
	MaxParallel int
}

Jump to

Keyboard shortcuts

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