Cryptographic Expander with GoIn most cryptographic hashing methods, we get a fixed length hash. For example, SHA1 gives us a 160-bit hash, and SHA-256 gives us a 256-bit hash. A cryptographic expander generates arbitrary bytes using either an XOF or a hash function. In this case we will use either SHA256 or SHA512 as a hashing fuction (as a Merkle-Damgård hash function), or SHAKE128 and SHA256 as an XOF (extendable output function) function |
Coding
The code is:
package main import ( "crypto" _ "crypto/sha256" _ "crypto/sha512" "fmt" "os" "strconv" "github.com/cloudflare/circl/expander" "github.com/cloudflare/circl/xof" ) func main() { method := "SHA256" Msg := "" size := "0x20" argCount := len(os.Args[1:]) if argCount > 0 { Msg = string(os.Args[1]) } if argCount > 1 { method = string(os.Args[2]) } if argCount > 2 { size = string(os.Args[3]) } var exp expander.Expander DST := "QUUX-V01-CS02-with-expander-SHA256-128" exp = expander.NewExpanderMD(crypto.SHA256, []byte(DST)) if method == "SHA256" { DST := "QUUX-V01-CS02-with-expander-SHA256-128" exp = expander.NewExpanderMD(crypto.SHA256, []byte(DST)) } if method == "SHA512" { DST := "QUUX-V01-CS02-with-expander-SHA512-256" exp = expander.NewExpanderMD(crypto.SHA512, []byte(DST)) } if method == "SHAKE128" { DST := "QUUX-V01-CS02-with-expander-SHAKE128" exp = expander.NewExpanderXOF(xof.SHAKE128, uint(128), []byte(DST)) } if method == "SHAKE256" { DST := "QUUX-V01-CS02-with-expander-SHAKE256" exp = expander.NewExpanderXOF(xof.SHAKE256, uint(256), []byte(DST)) } lenBytes, _ := strconv.ParseUint(size, 0, 64) got := exp.Expand([]byte(Msg), uint(lenBytes)) fmt.Printf("Message:\t%s\n", Msg) fmt.Printf("Domain string:\t%s\n", DST) fmt.Printf("Method:\t\t%s\n", method) fmt.Printf("\nLength:\t%d\n", lenBytes) fmt.Printf("Result: %x\n", got) }
A sample run is::
Message: abc Domain string: QUUX-V01-CS02-with-expander-SHA256-128 Method: SHA256 Length: 32 Result: d8ccab23b5985ccea865c6c97b6e5b8350e794e603b4b97902f53a8a0d605615