From df3469a79281028b6a7b93bb133e4c32e2fb9aa5 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 5 Jan 2015 17:12:36 -0500 Subject: [PATCH] Improve ShaHash.String comment and implementation. The hash is not displayed big endian, but rather as a byte-reversed hash as a hexidecimal string, so document it as such. While here, speed up the function. Some benchmarks: BenchmarkShaHashString 2000000 996 ns/op BenchmarkShaHashStringOld 100000 22701 ns/op --- shahash.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/shahash.go b/shahash.go index 1e3f8f21..3ce1e7b0 100644 --- a/shahash.go +++ b/shahash.go @@ -24,14 +24,13 @@ var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStr // typically represents the double sha256 of data. type ShaHash [HashSize]byte -// String returns the ShaHash in the standard bitcoin big-endian form. +// String returns the ShaHash as the hexidecimal string of the byte-reversed +// hash. func (hash ShaHash) String() string { - hashstr := "" - for i := range hash { - hashstr += fmt.Sprintf("%02x", hash[HashSize-1-i]) + for i := 0; i < HashSize/2; i++ { + hash[i], hash[HashSize-1-i] = hash[HashSize-1-i], hash[i] } - - return hashstr + return hex.EncodeToString(hash[:]) } // Bytes returns the bytes which represent the hash as a byte slice.