pbkdf2_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2012 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 pbkdf2
  5. import (
  6. "bytes"
  7. "crypto/sha1"
  8. "crypto/sha256"
  9. "hash"
  10. "testing"
  11. )
  12. type testVector struct {
  13. password string
  14. salt string
  15. iter int
  16. output []byte
  17. }
  18. // Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070
  19. var sha1TestVectors = []testVector{
  20. {
  21. "password",
  22. "salt",
  23. 1,
  24. []byte{
  25. 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  26. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  27. 0x2f, 0xe0, 0x37, 0xa6,
  28. },
  29. },
  30. {
  31. "password",
  32. "salt",
  33. 2,
  34. []byte{
  35. 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  36. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  37. 0xd8, 0xde, 0x89, 0x57,
  38. },
  39. },
  40. {
  41. "password",
  42. "salt",
  43. 4096,
  44. []byte{
  45. 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  46. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  47. 0x65, 0xa4, 0x29, 0xc1,
  48. },
  49. },
  50. // // This one takes too long
  51. // {
  52. // "password",
  53. // "salt",
  54. // 16777216,
  55. // []byte{
  56. // 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
  57. // 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
  58. // 0x26, 0x34, 0xe9, 0x84,
  59. // },
  60. // },
  61. {
  62. "passwordPASSWORDpassword",
  63. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  64. 4096,
  65. []byte{
  66. 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  67. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  68. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  69. 0x38,
  70. },
  71. },
  72. {
  73. "pass\000word",
  74. "sa\000lt",
  75. 4096,
  76. []byte{
  77. 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  78. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3,
  79. },
  80. },
  81. }
  82. // Test vectors from
  83. // http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
  84. var sha256TestVectors = []testVector{
  85. {
  86. "password",
  87. "salt",
  88. 1,
  89. []byte{
  90. 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c,
  91. 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37,
  92. 0xa8, 0x65, 0x48, 0xc9,
  93. },
  94. },
  95. {
  96. "password",
  97. "salt",
  98. 2,
  99. []byte{
  100. 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
  101. 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
  102. 0x2a, 0x30, 0x3f, 0x8e,
  103. },
  104. },
  105. {
  106. "password",
  107. "salt",
  108. 4096,
  109. []byte{
  110. 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41,
  111. 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d,
  112. 0x96, 0x28, 0x93, 0xa0,
  113. },
  114. },
  115. {
  116. "passwordPASSWORDpassword",
  117. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  118. 4096,
  119. []byte{
  120. 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
  121. 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
  122. 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
  123. 0x1c,
  124. },
  125. },
  126. {
  127. "pass\000word",
  128. "sa\000lt",
  129. 4096,
  130. []byte{
  131. 0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89,
  132. 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87,
  133. },
  134. },
  135. }
  136. func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []testVector) {
  137. for i, v := range vectors {
  138. o := Key([]byte(v.password), []byte(v.salt), v.iter, len(v.output), h)
  139. if !bytes.Equal(o, v.output) {
  140. t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o)
  141. }
  142. }
  143. }
  144. func TestWithHMACSHA1(t *testing.T) {
  145. testHash(t, sha1.New, "SHA1", sha1TestVectors)
  146. }
  147. func TestWithHMACSHA256(t *testing.T) {
  148. testHash(t, sha256.New, "SHA256", sha256TestVectors)
  149. }