s2k_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package s2k
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "crypto/sha1"
  9. "encoding/hex"
  10. "testing"
  11. )
  12. var saltedTests = []struct {
  13. in, out string
  14. }{
  15. {"hello", "10295ac1"},
  16. {"world", "ac587a5e"},
  17. {"foo", "4dda8077"},
  18. {"bar", "bd8aac6b9ea9cae04eae6a91c6133b58b5d9a61c14f355516ed9370456"},
  19. {"x", "f1d3f289"},
  20. {"xxxxxxxxxxxxxxxxxxxxxxx", "e00d7b45"},
  21. }
  22. func TestSalted(t *testing.T) {
  23. h := sha1.New()
  24. salt := [4]byte{1, 2, 3, 4}
  25. for i, test := range saltedTests {
  26. expected, _ := hex.DecodeString(test.out)
  27. out := make([]byte, len(expected))
  28. Salted(out, h, []byte(test.in), salt[:])
  29. if !bytes.Equal(expected, out) {
  30. t.Errorf("#%d, got: %x want: %x", i, out, expected)
  31. }
  32. }
  33. }
  34. var iteratedTests = []struct {
  35. in, out string
  36. }{
  37. {"hello", "83126105"},
  38. {"world", "6fa317f9"},
  39. {"foo", "8fbc35b9"},
  40. {"bar", "2af5a99b54f093789fd657f19bd245af7604d0f6ae06f66602a46a08ae"},
  41. {"x", "5a684dfe"},
  42. {"xxxxxxxxxxxxxxxxxxxxxxx", "18955174"},
  43. }
  44. func TestIterated(t *testing.T) {
  45. h := sha1.New()
  46. salt := [4]byte{4, 3, 2, 1}
  47. for i, test := range iteratedTests {
  48. expected, _ := hex.DecodeString(test.out)
  49. out := make([]byte, len(expected))
  50. Iterated(out, h, []byte(test.in), salt[:], 31)
  51. if !bytes.Equal(expected, out) {
  52. t.Errorf("#%d, got: %x want: %x", i, out, expected)
  53. }
  54. }
  55. }
  56. var parseTests = []struct {
  57. spec, in, out string
  58. }{
  59. /* Simple with SHA1 */
  60. {"0102", "hello", "aaf4c61d"},
  61. /* Salted with SHA1 */
  62. {"02020102030405060708", "hello", "f4f7d67e"},
  63. /* Iterated with SHA1 */
  64. {"03020102030405060708f1", "hello", "f2a57b7c"},
  65. }
  66. func TestParse(t *testing.T) {
  67. for i, test := range parseTests {
  68. spec, _ := hex.DecodeString(test.spec)
  69. buf := bytes.NewBuffer(spec)
  70. f, err := Parse(buf)
  71. if err != nil {
  72. t.Errorf("%d: Parse returned error: %s", i, err)
  73. continue
  74. }
  75. expected, _ := hex.DecodeString(test.out)
  76. out := make([]byte, len(expected))
  77. f(out, []byte(test.in))
  78. if !bytes.Equal(out, expected) {
  79. t.Errorf("%d: output got: %x want: %x", i, out, expected)
  80. }
  81. if testing.Short() {
  82. break
  83. }
  84. }
  85. }
  86. func TestSerialize(t *testing.T) {
  87. buf := bytes.NewBuffer(nil)
  88. key := make([]byte, 16)
  89. passphrase := []byte("testing")
  90. err := Serialize(buf, key, rand.Reader, passphrase)
  91. if err != nil {
  92. t.Errorf("failed to serialize: %s", err)
  93. return
  94. }
  95. f, err := Parse(buf)
  96. if err != nil {
  97. t.Errorf("failed to reparse: %s", err)
  98. return
  99. }
  100. key2 := make([]byte, len(key))
  101. f(key2, passphrase)
  102. if !bytes.Equal(key2, key) {
  103. t.Errorf("keys don't match: %x (serialied) vs %x (parsed)", key, key2)
  104. }
  105. }