sha1cd

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 19

README

sha1cd

A Go implementation of SHA1 with counter-cryptanalysis, which detects collision attacks.

The cgo/lib code is a carbon copy of the original code, based on the award winning white paper by Marc Stevens.

The Go and native implementations are largely based off upstream Go.

Usage

sha1cd can be used as a drop-in replacement for crypto/sha1:

import "github.com/pjbgf/sha1cd"

func test(){
	data := []byte("data to be sha1 hashed")
	h := sha1cd.Sum(data)
	fmt.Printf("hash: %q\n", hex.EncodeToString(h))
}

To obtain information as to whether a collision was found, use the func CollisionResistantSum.

import "github.com/pjbgf/sha1cd"

func test(){
	data := []byte("data to be sha1 hashed")
	h, col  := sha1cd.CollisionResistantSum(data)
	if col {
		fmt.Println("collision found!")
	}
	fmt.Printf("hash: %q", hex.EncodeToString(h))
}

Note that the algorithm will automatically avoid collision, by extending the SHA1 to 240-steps, instead of 80 when a collision attempt is detected. Therefore, inputs that contains the unavoidable bit conditions will yield a different hash from sha1cd, when compared with results using crypto/sha1. Valid inputs will have matching the outputs.

References

Use of the Original Implementation

Documentation

Overview

Package sha1cd implements collision detection based on the whitepaper Counter-cryptanalysis from Marc Stevens. The original ubc implementation was done by Marc Stevens and Dan Shumow, and can be found at: https://github.com/cr-marcstevens/sha1collisiondetection

Index

Constants

View Source
const BlockSize = shared.Chunk

The blocksize of SHA-1 in bytes.

View Source
const Size = shared.Size

The size of a SHA-1 checksum in bytes.

Variables

This section is empty.

Functions

func New

func New() hash.Hash

New returns a new hash.Hash computing the SHA1 checksum. The Hash also implements encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to marshal and unmarshal the internal state of the hash.

func Sum

func Sum(data []byte) ([Size]byte, bool)

Sum returns the SHA-1 checksum of the data.

Types

type CollisionResistantHash

type CollisionResistantHash interface {
	// CollisionResistantSum extends on Sum by returning an additional boolean
	// which indicates whether a collision was found during the hashing process.
	CollisionResistantSum(b []byte) ([]byte, bool)

	hash.Hash
}

Directories

Path Synopsis
cpu
Package cpu detects the CPU features that sha1cd dispatches on.
Package cpu detects the CPU features that sha1cd dispatches on.
ubc package provides ways for SHA1 blocks to be checked for Unavoidable Bit Conditions that arise from crypto analysis attacks.
ubc package provides ways for SHA1 blocks to be checked for Unavoidable Bit Conditions that arise from crypto analysis attacks.

Jump to

Keyboard shortcuts

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