From 046fed121a708792c0fed3ea3dcce3715b059b63 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 22 Apr 2018 01:12:58 +0200 Subject: [PATCH] [lib.scha] cleanup --- src/git.xdrm.io/schastsp/lib/scha/hash.go | 24 +++++++++-------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/git.xdrm.io/schastsp/lib/scha/hash.go b/src/git.xdrm.io/schastsp/lib/scha/hash.go index d585961..eadbca6 100644 --- a/src/git.xdrm.io/schastsp/lib/scha/hash.go +++ b/src/git.xdrm.io/schastsp/lib/scha/hash.go @@ -9,8 +9,8 @@ import ( /* (0) Static ---------------------------------------------------------*/ /* (1) Constants */ -const HBITSIZE uint32 = 512; -const HSIZE uint16 = uint16(HBITSIZE / 8); +const HSIZE uint16 = sha512.Size; +const HBITSIZE uint32 = uint32(HSIZE) * 8; @@ -27,14 +27,15 @@ func hash(input []byte) []byte{ /* (1) Create sha512 hasher */ hasher := sha512.New(); - /* (2) Set input to be hashed */ + /* (2) Defer memory cleanup */ + defer hasher.Reset(); + + /* (3) Set input to be hashed */ hasher.Write(input); - /* (3) Extract digest */ + /* (4) Extract digest */ return hasher.Sum(nil); - // digest := base64.StdEncoding.EncodeToString(container) - // fmt.Println(digest) } @@ -70,24 +71,17 @@ func Hash(input []byte, depth uint, salt []byte, pepper []byte) ([]byte, error) /* (2) Process first hash + salt */ digest = hash( xor.ByteArray(input, salt) ) - if depth > 0 { depth-- } /* (3) Iterate @depth times */ - for depth > 0 { + for depth--; depth > 0; depth-- { - // 1. Add Pepper for last time + // Add Pepper only for last time if( depth == 1 ){ - digest = hash( xor.ByteArray(digest, pepper) ) - - // 2. Else only hash } else { digest = hash(digest) - } - depth--; - } return digest, nil