shasum

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: Apache-2.0 Imports: 7 Imported by: 18

README

godoc codecov Go Report Card

shasum - secure hash algorithm utilities

A collection of utilities for working with secure hashes.

Installation

> go get github.com/go-corelibs/shasum@latest

Examples

Hash, Sum, BriefSum

func main() {
    shasum, err := shasum.Hash([]byte{}, shasum.Sha256Type)
    // err == nil
    // shasum == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    shasum, err = shasum.Sum([]byte{})
    // err == nil
    // shasum == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    shasum, err = shasum.BriefSum([]byte{})
    // err == nil
    // shasum == "e3b0c44298"
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package shasum provides secure hash algorithm utilities

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultType = Sha256Type
)
View Source
var (
	ErrVerifyFailed = errors.New("shasum verification failed")
)

Functions

func BriefFile

func BriefFile(file string) (shasum string, err error)

BriefFile is a wrapper around File which returns a BriefLength `shasum`

func BriefSum

func BriefSum[V HashInputTypes](data V) (shasum string, err error)

BriefSum is a wrapper around Sum which returns a BriefLength `shasum`

func File

func File(file string) (shasum string, err error)

File is a convenience wrapper around HashFile using the DefaultType

func Hash

func Hash[V HashInputTypes](data V, t HashType) (shasum string, err error)

Hash calculates the shasum of the given `data` and returns the hexadecimal encoded value

BriefType is an alias of Sha256Type and if an unknown HashType is given, Sha256Type is used

func HashFile

func HashFile(file string, t HashType) (shasum string, err error)

HashFile is a convenience wrapper around reading the `file` data and returning the result of a call to Hash

func MustBriefFile

func MustBriefFile(file string) (shasum string)

MustBriefFile is a wrapper around File which returns a BriefLength `shasum`

func MustBriefSum

func MustBriefSum[V HashInputTypes](data V) (shasum string)

MustBriefSum is a wrapper around BriefSum which panics with any error

func MustFile

func MustFile(file string) (shasum string)

MustFile is a convenience wrapper around MustHashFile using the DefaultType

func MustHash

func MustHash[V HashInputTypes](data V, t HashType) (shasum string)

MustHash is a convenience wrapper around Hash which panics with any error

func MustHashFile

func MustHashFile(file string, t HashType) (shasum string)

MustHashFile is a convenience wrapper around HashFile which panics with any error

func MustSum

func MustSum[V HashInputTypes](data V) (shasum string)

MustSum is a wrapper around Sum which panics with any error

func MustSum224

func MustSum224[V HashInputTypes](data V) (shasum string)

MustSum224 is a wrapper around Sum224 which panics with any error

func MustSum256

func MustSum256[V HashInputTypes](data V) (shasum string)

MustSum256 is a wrapper around Sum256 which panics with any error

func MustSum384

func MustSum384[V HashInputTypes](data V) (shasum string)

MustSum384 is a wrapper around Sum384 which panics with any error

func MustSum512

func MustSum512[V HashInputTypes](data V) (shasum string)

MustSum512 is a wrapper around Sum512 which panics with any error

func Sha1Bytes

func Sha1Bytes(data []byte) (shasum [20]byte)

Sha1Bytes is a convenience wrapper around sha1.Sum

Note: do not use SHA-1 for securing data

func Sha1Sum

func Sha1Sum(data []byte) (shasum string)

Sha1Sum is a convenience wrapper around Sha1Bytes, returning the `shasum` as a hexadecimal string instead of a byte array

Note: do not use SHA-1 for securing data

func Sum

func Sum[V HashInputTypes](data V) (shasum string, err error)

Sum is a wrapper around Hash using the DefaultType

func Sum224

func Sum224[V HashInputTypes](data V) (shasum string, err error)

Sum224 uses SHA-224 to hash the given `data` and return the result as a hexadecimal encoded string of Sha224Length

func Sum256

func Sum256[V HashInputTypes](data V) (shasum string, err error)

Sum256 uses SHA-256 to hash the given `data` and return the result as a hexadecimal encoded string of Sha256Length

func Sum384

func Sum384[V HashInputTypes](data V) (shasum string, err error)

Sum384 uses SHA-384 to hash the given `data` and return the result as a hexadecimal encoded string of Sha384Length

func Sum512

func Sum512[V HashInputTypes](data V) (shasum string, err error)

Sum512 uses SHA-512 to hash the given `data` and return the result as a hexadecimal encoded string of Sha512Length

func Valid

func Valid(hash string) (valid bool)

Valid checks if the `hash` given is a valid HashLength string that passes a call to Validate

func Validate

func Validate(hash string, size HashLength) (valid bool)

Validate returns true if the given `hash` is exactly the HashLength required and contains only the digits 0-9 and the lower-case letters a-f

func Verify

func Verify(hash string, data []byte) (err error)

Verify compares the given `hash` matches the exact Sum result derived from the given `data`

func VerifyFile

func VerifyFile(hash, file string) (err error)

VerifyFile uses Verify to validate the given file's integrity

Types

type HashInputTypes

type HashInputTypes interface {
	~[]byte | ~string
}

HashInputTypes is the generic constraint for byte slice and string things

type HashLength

type HashLength uint

HashLength is the length of a hexadecimal encoded string produced from one of the secure hashing functions

const (
	// BriefLength is accepted as a short-form Sha256Length
	BriefLength  HashLength = 10
	Sha224Length HashLength = 56
	Sha256Length HashLength = 64
	Sha384Length HashLength = 96
	Sha512Length HashLength = 128
)

type HashType

type HashType uint

HashType is used to specify the DefaultType used by Sum and File

const (
	// BriefType is an alias for Sha256Type
	BriefType  HashType = 10
	Sha224Type HashType = 224
	Sha256Type HashType = 256
	Sha384Type HashType = 384
	Sha512Type HashType = 512
)

Jump to

Keyboard shortcuts

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