crypto_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package crypto
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. "testing"
  6. )
  7. const (
  8. password = "test_password"
  9. kdfSalt = "vd0dcYMGNLKn/gpT6uTFTw=="
  10. memLimit = 64 * 1024 * 1024 // 64MB
  11. opsLimit = 2
  12. cipherText = "kBXQ2PuX6y/aje5r22H0AehRPh6sQ0ULoeAO"
  13. cipherNonce = "v7wsI+BFZsRMIjDm3rTxPhmi/CaUdkdJ"
  14. expectedPlainText = "plain_text"
  15. expectedDerivedKey = "vp8d8Nee0BbIML4ab8Cp34uYnyrN77cRwTl920flyT0="
  16. )
  17. func TestDeriveArgonKey(t *testing.T) {
  18. derivedKey, err := DeriveArgonKey(password, kdfSalt, memLimit, opsLimit)
  19. if err != nil {
  20. t.Fatalf("Failed to derive key: %v", err)
  21. }
  22. if base64.StdEncoding.EncodeToString(derivedKey) != expectedDerivedKey {
  23. t.Fatalf("Derived key does not match expected key")
  24. }
  25. }
  26. func TestDecryptChaCha20poly1305(t *testing.T) {
  27. derivedKey, err := DeriveArgonKey(password, kdfSalt, memLimit, opsLimit)
  28. if err != nil {
  29. t.Fatalf("Failed to derive key: %v", err)
  30. }
  31. decodedCipherText, err := base64.StdEncoding.DecodeString(cipherText)
  32. if err != nil {
  33. t.Fatalf("Failed to decode cipher text: %v", err)
  34. }
  35. decodedCipherNonce, err := base64.StdEncoding.DecodeString(cipherNonce)
  36. if err != nil {
  37. t.Fatalf("Failed to decode cipher nonce: %v", err)
  38. }
  39. decryptedText, err := decryptChaCha20poly1305(decodedCipherText, derivedKey, decodedCipherNonce)
  40. if err != nil {
  41. t.Fatalf("Failed to decrypt: %v", err)
  42. }
  43. if string(decryptedText) != expectedPlainText {
  44. t.Fatalf("Decrypted text : %s does not match the expected text: %s", string(decryptedText), expectedPlainText)
  45. }
  46. }
  47. func TestEncryptAndDecryptChaCha20Ploy1305(t *testing.T) {
  48. key := make([]byte, 32)
  49. _, err := rand.Read(key)
  50. if err != nil {
  51. t.Fatalf("Failed to generate random key: %v", err)
  52. }
  53. cipher, nonce, err := EncryptChaCha20poly1305([]byte("plain_text"), key)
  54. if err != nil {
  55. return
  56. }
  57. plainText, err := decryptChaCha20poly1305(cipher, key, nonce)
  58. if err != nil {
  59. t.Fatalf("Failed to decrypt: %v", err)
  60. }
  61. if string(plainText) != "plain_text" {
  62. t.Fatalf("Decrypted text : %s does not match the expected text: %s", string(plainText), "plain_text")
  63. }
  64. }
  65. func TestSecretBoxOpenBase64(t *testing.T) {
  66. sealedCipherText := "KHwRN+RzvTu+jC7mCdkMsqnTPSLvevtZILmcR2OYFbIRPqDyjAl+m8KxD9B5fiEo"
  67. sealNonce := "jgfPDOsQh2VdIHWJVSBicMPF2sQW3HIY"
  68. sealKey, _ := base64.StdEncoding.DecodeString("kercNpvGufMTTHmDwAhz26DgCAvznd1+/buBqKEkWr4=")
  69. expectedSealedText := "O1ObUBMv+SCE1qWHD7+WViEIZcAeTp18Y+m9eMlDE1Y="
  70. plainText, err := SecretBoxOpenBase64(sealedCipherText, sealNonce, sealKey)
  71. if err != nil {
  72. t.Fatalf("Failed to decrypt: %v", err)
  73. }
  74. if expectedSealedText != base64.StdEncoding.EncodeToString(plainText) {
  75. t.Fatalf("Decrypted text : %s does not match the expected text: %s", string(plainText), expectedSealedText)
  76. }
  77. }