pbkdf2_test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.crypt.pbkdf2Test');
  15. goog.setTestOnly('goog.crypt.pbkdf2Test');
  16. goog.require('goog.crypt');
  17. goog.require('goog.crypt.pbkdf2');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. function testPBKDF2() {
  21. // PBKDF2 test vectors from:
  22. // http://tools.ietf.org/html/rfc6070
  23. if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('7')) {
  24. return;
  25. }
  26. var testPassword = goog.crypt.stringToByteArray('password');
  27. var testSalt = goog.crypt.stringToByteArray('salt');
  28. assertElementsEquals(
  29. goog.crypt.hexToByteArray('0c60c80f961f0e71f3a9b524af6012062fe037a6'),
  30. goog.crypt.pbkdf2.deriveKeySha1(testPassword, testSalt, 1, 160));
  31. assertElementsEquals(
  32. goog.crypt.hexToByteArray('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'),
  33. goog.crypt.pbkdf2.deriveKeySha1(testPassword, testSalt, 2, 160));
  34. assertElementsEquals(
  35. goog.crypt.hexToByteArray('4b007901b765489abead49d926f721d065a429c1'),
  36. goog.crypt.pbkdf2.deriveKeySha1(testPassword, testSalt, 4096, 160));
  37. testPassword = goog.crypt.stringToByteArray('passwordPASSWORDpassword');
  38. testSalt =
  39. goog.crypt.stringToByteArray('saltSALTsaltSALTsaltSALTsaltSALTsalt');
  40. assertElementsEquals(
  41. goog.crypt.hexToByteArray(
  42. '3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038'),
  43. goog.crypt.pbkdf2.deriveKeySha1(testPassword, testSalt, 4096, 200));
  44. testPassword = goog.crypt.stringToByteArray('pass\0word');
  45. testSalt = goog.crypt.stringToByteArray('sa\0lt');
  46. assertElementsEquals(
  47. goog.crypt.hexToByteArray('56fa6aa75548099dcc37d7f03425e0c3'),
  48. goog.crypt.pbkdf2.deriveKeySha1(testPassword, testSalt, 4096, 128));
  49. }