Exemple #1
0
// TestPasswordSanity makes sure EncryptPassword and ComparePassword work well together
func TestPasswordSanity(t *testing.T) {
	pw := make([]byte, 10)
	pw2 := make([]byte, 10)
	var salt, hash, hash2 []byte

	for i := 0; i < 32; i++ {
		rand.Read(pw)
		rand.Read(pw2)
		salt = Salt128()
		hash = EncryptPassword(pw, salt)
		hash2 = EncryptPassword(pw2, salt)

		if err := ComparePassword(hash, pw, salt); err != nil {
			t.Error(err)
			t.FailNow()
		}
		if err := ComparePassword(hash2, pw, salt); err == nil {
			t.Error("ComparePassword is giving false positive")
			t.FailNow()
		}
	}
}
Exemple #2
0
// Salt128 generates 128 bits of random data.
func Salt128() []byte {
	x := make([]byte, 16)
	rand.Read(x)
	return x
}