From cf6ebcda87eaa606d9e4fb32f369286657360a7b Mon Sep 17 00:00:00 2001 From: Michael Meier Date: Fri, 22 Jan 2010 15:25:02 +0100 Subject: [PATCH] utility.go: cleanup --- lib/utility.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/utility.go b/lib/utility.go index 5d6fe74..563fcea 100644 --- a/lib/utility.go +++ b/lib/utility.go @@ -7,19 +7,21 @@ import ( ) -type Distance []byte; +type Distance []byte -func (d Distance) String() string { - return hex.EncodeToString(d) -} +func (d Distance) String() string { return hex.EncodeToString(d) } + const K = 20 + +// Hashes a byte slice and returns a 20 byte string. func SHA1Bytes(b []byte) string { h := sha1.New() h.Write(b) return string(h.Sum()) } +// Hashes a string and returns a 20 byte string. func SHA1String(s string) string { h := sha1.New() h.Write(strings.Bytes(s)) @@ -32,10 +34,10 @@ func XOR(a, b string) Distance { if l != len(b) { return nil } - + d := make(Distance, l) - for i := 0; i < l; i ++ { + for i := 0; i < l; i++ { d[i] = a[i] ^ b[i] } @@ -56,14 +58,14 @@ func BucketNo(d Distance) uint { continue } var bitnr uint = 0 - for i := 0; i < 8; i ++ { + for i := 0; i < 8; i++ { if (b & (0x80 >> bitnr)) != 0 { return basebitnr + bitnr } bitnr++ } } - + return basebitnr } @@ -75,7 +77,7 @@ func (a Distance) Less(b Distance) bool { //fmt.Printf("distance Less called: %v < %v\n", a, b) - for i, ea := range(a) { + for i, ea := range a { eb := b[i] switch { case ea < eb: