File: md5_crypt_test.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (88 lines) | stat: -rw-r--r-- 1,932 bytes parent folder | download
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 md5_crypt

import "testing"

var md5Crypt = New()

func TestGenerate(t *testing.T) {
	data := []struct {
		salt []byte
		key  []byte
		out  string
	}{
		{
			[]byte("$1$$"),
			[]byte("abcdefghijk"),
			"$1$$pL/BYSxMXs.jVuSV1lynn1",
		},
		{
			[]byte("$1$an overlong salt$"),
			[]byte("abcdfgh"),
			"$1$an overl$ZYftmJDIw8sG5s4gG6r.70",
		},
		{
			[]byte("$1$12345678$"),
			[]byte("Lorem ipsum dolor sit amet"),
			"$1$12345678$Suzx8CrBlkNJwVHHHv5tZ.",
		},
		{
			[]byte("$1$deadbeef$"),
			[]byte("password"),
			"$1$deadbeef$Q7g0UO4hRC0mgQUQ/qkjZ0",
		},
		{
			[]byte("$1$$"),
			[]byte("missing salt"),
			"$1$$Lv61fbMiEGprscPkdE9Iw/",
		},
		{
			[]byte("$1$holy-moly-batman$"),
			[]byte("1234567"),
			"$1$holy-mol$WKomB0dWknSxdW/e8WYHG0",
		},
		{
			[]byte("$1$asdfjkl;$"),
			[]byte("A really long password. Longer " +
				"than a password has any right to be" +
				". Hey bub, don't mess with this password."),
			"$1$asdfjkl;$DUqPhKwbK4smV0aEMyDdx/",
		},
	}

	for i, d := range data {
		hash, err := md5Crypt.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 := md5Crypt.Generate(d, nil)
		if err != nil {
			t.Fatal(err)
		}
		if err = md5Crypt.Verify(hash, d); err != nil {
			t.Errorf("Test %d failed: %s", i, d)
		}
	}
}