sqlite3_func_crypt_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. import (
  7. "fmt"
  8. "strings"
  9. "testing"
  10. )
  11. // TestCryptEncoders to increase coverage
  12. func TestCryptEncoders(t *testing.T) {
  13. tests := []struct {
  14. enc string
  15. salt string
  16. expected string
  17. }{
  18. {"sha1", "", "d033e22ae348aeb5660fc2140aec35850c4da997"},
  19. {"sha256", "", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"},
  20. {"sha384", "", "9ca694a90285c034432c9550421b7b9dbd5c0f4b6673f05f6dbce58052ba20e4248041956ee8c9a2ec9f10290cdc0782"},
  21. {"sha512", "", "c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec"},
  22. {"ssha1", "salt", "9bc7aa55f08fdad935c3f8362d3f48bcf70eb280"},
  23. {"ssha256", "salt", "f9a81477552594c79f2abc3fc099daa896a6e3a3590a55ffa392b6000412e80b"},
  24. {"ssha384", "salt", "9ed776b477fcfc1b5e584989e8d770f5e17d98a7643546a63c2b07d4ab00f1348f6b8e73103d3a23554f727136e8c215"},
  25. {"ssha512", "salt", "3c4a79782143337be4492be072abcfe979dd703c00541a8c39a0f3df4bab2029c050cf46fddc47090b5b04ac537b3e78189e3de16e601e859f95c51ac9f6dafb"},
  26. }
  27. for _, e := range tests {
  28. var fn func(pass []byte, hash interface{}) []byte
  29. switch e.enc {
  30. case "sha1":
  31. fn = CryptEncoderSHA1
  32. case "ssha1":
  33. fn = CryptEncoderSSHA1(e.salt)
  34. case "sha256":
  35. fn = CryptEncoderSHA256
  36. case "ssha256":
  37. fn = CryptEncoderSSHA256(e.salt)
  38. case "sha384":
  39. fn = CryptEncoderSHA384
  40. case "ssha384":
  41. fn = CryptEncoderSSHA384(e.salt)
  42. case "sha512":
  43. fn = CryptEncoderSHA512
  44. case "ssha512":
  45. fn = CryptEncoderSSHA512(e.salt)
  46. }
  47. h := fn([]byte("admin"), nil)
  48. if strings.Compare(fmt.Sprintf("%x", h), e.expected) != 0 {
  49. t.Fatalf("Invalid %s hash: expected: %s; got: %x", strings.ToUpper(e.enc), e.expected, h)
  50. }
  51. }
  52. }