fdh

package module
v0.0.0-...-5eb31ce Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2017 License: BSD-3-Clause Imports: 5 Imported by: 3

README

Full Domain Hash

Build Status Build Status Go Report Card Coverage Status Scrutinizer Issues GoDoc

A Full Domain Hash (FDH) is a useful cryptographic construction that extends the size of a hash digest to an arbitrary length. For example, SHA256 can be expanded to 1024 bits instead of the usual 256 bits.

We construct an FDH by computing a number of cycles where cycles=(target length)/(digest length) + 1 We then compute FDH(M) = HASH(M||0)||HASH(M||1)||...||HASH(M||cycles−1), where HASH is any hash function defined in package crypto, || denotes concatenation, and numerical values are binary (\x01, \x02 etc).

FDHs are usually used with an RSA signature scheme where the target length is the size of the key. See https://en.wikipedia.org/wiki/Full_Domain_Hash

Example

import (
	"crypto"
	_ "crypto/sha256"
	"github.com/cryptoballot/fdh"
	"fmt"
	"encoding/hex"
)

var message = []byte("ATTACK AT DAWN")

func main() {
	h := fdh.New(crypto.SHA256, 2048)
	h.Write(message)
	digest := h.Sum(nil)
	
	// We now have a SHA256 digest that has been extended from 256 bits to 2048 bits.
	fmt.Println(hex.EncodeToString(digest))
}

As a shortcut you can also use the Sum function.

import (
	"crypto"
	_ "crypto/md5"
	"github.com/cryptoballot/fdh"
	"log"
)

var message = []byte("ATTACK AT DAWN")

func main() {
	digest := fdh.Sum(crypto.MD5, 1024, message)
	// ... do something with digest ...
}

Bash equivalent

# Expand SHA256 hash of "ATTACK AT DAWN" to 1024 bits
echo -n -e 'ATTACK AT DAWN\x00' | shasum -a 256 | cut -d ' ' -f 1 | tr -d '\n' &&\
echo -n -e 'ATTACK AT DAWN\x01' | shasum -a 256 | cut -d ' ' -f 1 | tr -d '\n' &&\
echo -n -e 'ATTACK AT DAWN\x02' | shasum -a 256 | cut -d ' ' -f 1 | tr -d '\n' &&\
echo -n -e 'ATTACK AT DAWN\x03' | shasum -a 256 | cut -d ' ' -f 1

Documentation

Overview

Package fdh implements a full domain hash.

A Full Domain Hash (FDH) is a useful cryptographic construction that extends the size of a hash digest to an arbitrary length

We construct an FDH by computing a number of cycles where cycles=(target length)/(digest length) + 1We then compute FDH(M) = HASH(M||0)||HASH(M||1)||...||HASH(M||cycles−1), where HASH is any hash function defined in package crypto, || denotes concatenation, and numerical values are binary (\x01, \x02 etc).

FDHs are usually used with an RSA signature scheme where the target length is the size of the key. See https://en.wikipedia.org/wiki/Full_Domain_Hash

Example

import (
	"crypto"
	_ "crypto/sha256"
	"github.com/cryptoballot/fdh"
	"fmt"
	"encoding/hex"
)

var message = []byte("ATTACK AT DAWN")

func main() {
	h := fdh.New(crypto.SHA256, 2048)
	h.Write(message)
	digest := h.Sum(nil)

	// We now have a SHA256 digest that has been extended from 256 bits to 2048 bits.
	fmt.Println(hex.EncodeToString(digest))
}

As a shortcut you can also use the Sum function.

import (
	"crypto"
	_ "crypto/md5"
	"github.com/cryptoballot/fdh"
	"log"
)

var message = []byte("ATTACK AT DAWN")

func main() {
	digest := fdh.Sum(crypto.MD5, 1024, message)
	// ... do something with digest ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(h crypto.Hash, bitlen int) hash.Hash

New returns a hash.Hash for computing a Full Domain Hash checksum, given a base hash function and a target bit length. It will panic if the bitlen is not a multiple of the hash length or if the hash library is not imported.

func Sum

func Sum(h crypto.Hash, bitlen int, message []byte) []byte

Sum returns the the Full Domain Hash checksum of the data.

Types

This section is empty.

Jump to

Keyboard shortcuts

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