-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff997a8
commit fc56475
Showing
5 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,6 @@ _testmain.go | |
|
||
# Cloud 9 | ||
.c9revisions/ | ||
|
||
# Sublime | ||
*.sublime-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
package passwords | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"hash" | ||
) | ||
|
||
// Passwords for MySql | ||
func MySQL(pass string) string { | ||
// Create our own hasher, for thread safety | ||
hasher := sha1.New() | ||
// Get our own hasher, for thread safety | ||
hasher := sha1Pool.Get().(hash.Hash) | ||
// Back to the pool! (later) | ||
defer sha1Pool.Put(hasher) | ||
|
||
// We cannot assume the hasher from the pool is clean | ||
hasher.Reset() | ||
|
||
// This is actually really simple, it's just a sha1 of a sha1 displayed as hex | ||
// (with a leading * to indicate the 'new' password hash of MySql) | ||
hasher.Write([]byte(pass)) | ||
interm := hasher.Sum(nil) | ||
hasher.Reset() | ||
hasher.Write(interm) | ||
|
||
return "*" + hex.EncodeToString(hasher.Sum(nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package passwords | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const ( | ||
mySQLFail = "Password '%s' encrypted by MySQL should have been '%s'; got '%s'." | ||
) | ||
|
||
var ( | ||
validMySQLPasswords = map[string]string{ | ||
"Password1": "*7ee969bbe0a3985c8bff9fa65a06345c67fe434a", | ||
"password": "*2470c0c06dee42fd1618bb99005adca2ec9d1e19", | ||
"recover": "*71383c96a85618165875eb3f9d9e52179c3291e7", | ||
"incorrect": "*2f8bcdbfd7618656393be67f7f1d8ccfaa87f8fc", | ||
} | ||
) | ||
|
||
func TestMySQL(t *testing.T) { | ||
for plain, encrypted := range validMySQLPasswords { | ||
got := MySQL(plain) | ||
if encrypted != got { | ||
t.Fatalf(mySQLFail, plain, encrypted, got) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMySQL(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
MySQL("A slightly longer thing to encrypt") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"folders": | ||
[ | ||
{ | ||
"follow_symlinks": true, | ||
"path": "." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package passwords | ||
|
||
import ( | ||
"crypto/sha1" | ||
"sync" | ||
) | ||
|
||
// This block contains various pools for resources we constantly need; mosty hashers and encoders. | ||
var ( | ||
sha1Pool = sync.Pool{ | ||
New: sha1New, | ||
} | ||
) | ||
|
||
func sha1New() interface{} { | ||
return sha1.New() | ||
} |