passwd_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2019 Yunion
  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. package seclib2
  15. import "testing"
  16. func TestGeneratePassword(t *testing.T) {
  17. passwd := "Hello world!"
  18. dk, err := GeneratePassword(passwd)
  19. if err != nil {
  20. t.Errorf("%s", err)
  21. return
  22. }
  23. t.Logf("%s", dk)
  24. err = VerifyPassword(passwd, dk)
  25. if err != nil {
  26. t.Errorf("fail to verify %s", err)
  27. }
  28. }
  29. func TestGeneratePassword2(t *testing.T) {
  30. passwd := "Hello world!"
  31. dk, err := BcryptPassword(passwd)
  32. if err != nil {
  33. t.Errorf("%s", err)
  34. return
  35. }
  36. t.Logf("%s", dk)
  37. err = BcryptVerifyPassword(passwd, dk)
  38. if err != nil {
  39. t.Errorf("fail to verify %s", err)
  40. }
  41. hash := "$2b$12$PhhOkNNNa2wWU643XKVC3uS6cVR8JY4ZkJ2p.GlmZWCiv7oqp2a9m"
  42. pass := "MxqhTC2VKe067jtD"
  43. err = BcryptVerifyPassword(pass, hash)
  44. if err != nil {
  45. t.Errorf("Verify existing fail %s", err)
  46. }
  47. }