You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unsalted_test.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package passwd
  4. import (
  5. "testing"
  6. )
  7. var UnsaltedPasswords = map[string]string{
  8. "test1": "JDJhJDA0JFFwZ1V0RWZTMFVaMkFrdlRrTG9FZk9FNEZWbWkvVEhsdGFnSXlIUC5wVmpYTkNERFJPNlcu",
  9. "test2": "JDJhJDA0JHpQTGNqczlIanc3V2NFQ3JEOVlTM09aNkRTbGRsQzRyNmt3Q01aSUs2Y2xyWURVODZ1V0px",
  10. "supernomo": "JDJhJDA0JHdJekhnQmk1VXQ4WUphL0pIL0tXQWVKVXJ6dXcvRDJ3WFljWW9XOGhzNllIbW1DRlFkL1VL",
  11. }
  12. func TestUnsaltedPassword(t *testing.T) {
  13. for password, hash := range UnsaltedPasswords {
  14. generatedHash, err := GenerateEncodedPassword(password)
  15. if err != nil {
  16. t.Errorf("Could not hash password for [%s]: %s", password, err.Error())
  17. }
  18. hashBytes, err := DecodePasswordHash(hash)
  19. if err != nil {
  20. t.Errorf("Could not decode hash for [%s]: %s", password, err.Error())
  21. }
  22. generatedHashBytes, err := DecodePasswordHash(generatedHash)
  23. if err != nil {
  24. t.Errorf("Could not decode generated hash for [%s]: %s", password, err.Error())
  25. }
  26. passwordBytes := []byte(password)
  27. if ComparePassword(hashBytes, passwordBytes) != nil {
  28. t.Errorf("Stored hash for [%s] did not match", password)
  29. }
  30. if ComparePassword(generatedHashBytes, passwordBytes) != nil {
  31. t.Errorf("Generated hash for [%s] did not match", password)
  32. }
  33. }
  34. }
  35. func TestUnsaltedPasswordFailures(t *testing.T) {
  36. _, err := GenerateEncodedPassword("")
  37. if err != ErrEmptyPassword {
  38. t.Error("Generating empty password did not fail as expected!")
  39. }
  40. _, err = DecodePasswordHash("")
  41. if err != ErrEmptyPassword {
  42. t.Error("Decoding empty password hash did not fail as expected!")
  43. }
  44. }