Skip to content

Commit

Permalink
add pool to ShaCrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisGuyCodes committed Dec 25, 2014
1 parent 4eef934 commit 680e098
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ShaCrypt.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package passwords

import (
"crypto/sha512"
"errors"
"hash"
"math/big"
)

Expand Down Expand Up @@ -32,7 +32,13 @@ func ShaCrypt(pass, salt string) (string, error) {
}

// Create the hasher (unique per instance to be thread safe :D)
hasher := sha512.New()
hasher := sha512Pool.Get().(hash.Hash)
// Return to the pool! (later)
defer sha512Pool.Put(hasher)

// We can't know this hasher is fresh
hasher.Reset()

hashLen := hasher.Size()

// Change to bytes, more naitive
Expand Down
8 changes: 8 additions & 0 deletions pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package passwords

import (
"crypto/sha1"
"crypto/sha512"
"sync"
)

Expand All @@ -10,8 +11,15 @@ var (
sha1Pool = sync.Pool{
New: sha1New,
}
sha512Pool = sync.Pool{
New: sha512New,
}
)

func sha1New() interface{} {
return sha1.New()
}

func sha512New() interface{} {
return sha512.New()
}

0 comments on commit 680e098

Please sign in to comment.