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.

52 lines
1.0 KiB

  1. package crypto
  2. import (
  3. "bytes"
  4. "testing"
  5. "golang.org/x/crypto/bcrypt"
  6. )
  7. func TestSimple(t *testing.T) {
  8. MixEntropy([]byte("someentropy"))
  9. plaintext := []byte("sometext")
  10. secret := []byte("somesecretoflengththirtytwo===32")
  11. ciphertext := EncryptSymmetric(plaintext, secret)
  12. plaintext2, err := DecryptSymmetric(ciphertext, secret)
  13. if err != nil {
  14. t.Error(err)
  15. }
  16. if !bytes.Equal(plaintext, plaintext2) {
  17. t.Errorf("Decrypted plaintext was %X, expected %X", plaintext2, plaintext)
  18. }
  19. }
  20. func TestSimpleWithKDF(t *testing.T) {
  21. MixEntropy([]byte("someentropy"))
  22. plaintext := []byte("sometext")
  23. secretPass := []byte("somesecret")
  24. secret, err := bcrypt.GenerateFromPassword(secretPass, 12)
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. secret = Sha256(secret)
  29. ciphertext := EncryptSymmetric(plaintext, secret)
  30. plaintext2, err := DecryptSymmetric(ciphertext, secret)
  31. if err != nil {
  32. t.Error(err)
  33. }
  34. if !bytes.Equal(plaintext, plaintext2) {
  35. t.Errorf("Decrypted plaintext was %X, expected %X", plaintext2, plaintext)
  36. }
  37. }