1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
// Copyright 2012, Jeramey Crawford <jeramey@antihe.ro>
// Copyright 2013, Jonas mg
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.
package apr1_crypt
import "testing"
var apr1Crypt = New()
func TestGenerate(t *testing.T) {
data := []struct {
salt []byte
key []byte
out string
}{
{
[]byte("$apr1$$"),
[]byte("abcdefghijk"),
"$apr1$$NTjzQjNZnhYRPxN6ryN191",
},
{
[]byte("$apr1$an overlong salt$"),
[]byte("abcdefgh"),
"$apr1$an overl$iroRZrWCEoQojCkf6p8LC0",
},
{
[]byte("$apr1$12345678$"),
[]byte("Lorem ipsum dolor sit amet"),
"$apr1$12345678$/DpfgRGBHG8N0cbkmw0Fk/",
},
{
[]byte("$apr1$deadbeef$"),
[]byte("password"),
"$apr1$deadbeef$NWLhx1Ai4ScyoaAboTFco.",
},
{
[]byte("$apr1$$"),
[]byte("missing salt"),
"$apr1$$EcorjwkoQz4mYcksVEk6j0",
},
{
[]byte("$apr1$holy-moly-batman$"),
[]byte("1234567"),
"$apr1$holy-mol$/WX0350ZUEkvQkrrVJsrU.",
},
{
[]byte("$apr1$asdfjkl;$"),
[]byte("A really long password. " +
"Longer than a password has any righ" +
"t to be. Hey bub, don't mess with t" +
"his password."),
"$apr1$asdfjkl;$2MbDUb/Bj6qcIIf38PXzp0",
},
}
for i, d := range data {
hash, err := apr1Crypt.Generate(d.key, d.salt)
if err != nil {
t.Fatal(err)
}
if hash != d.out {
t.Errorf("Test %d failed\nExpected: %s, got: %s", i, d.out, hash)
}
}
}
func TestVerify(t *testing.T) {
data := [][]byte{
[]byte("password"),
[]byte("12345"),
[]byte("That's amazing! I've got the same combination on my luggage!"),
[]byte("And change the combination on my luggage!"),
[]byte(" random spa c ing."),
[]byte("94ajflkvjzpe8u3&*j1k513KLJ&*()"),
}
for i, d := range data {
hash, err := apr1Crypt.Generate(d, nil)
if err != nil {
t.Fatal(err)
}
if err = apr1Crypt.Verify(hash, d); err != nil {
t.Errorf("Test %d failed: %s", i, d)
}
}
}
|