crypto_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package utils_test
  16. import (
  17. . "github.com/onsi/ginkgo"
  18. . "github.com/onsi/gomega"
  19. . "github.com/mudler/edgevpn/pkg/utils"
  20. )
  21. var _ = Describe("Crypto utilities", func() {
  22. Context("AES", func() {
  23. It("Encode/decode", func() {
  24. key := RandStringRunes(32)
  25. message := "foo"
  26. k := [32]byte{}
  27. copy([]byte(key)[:], k[:32])
  28. encoded, err := AESEncrypt(message, &k)
  29. Expect(err).ToNot(HaveOccurred())
  30. Expect(encoded).ToNot(Equal(key))
  31. Expect(len(encoded)).To(Equal(62))
  32. // Encode again
  33. encoded2, err := AESEncrypt(message, &k)
  34. Expect(err).ToNot(HaveOccurred())
  35. // should differ
  36. Expect(encoded2).ToNot(Equal(encoded))
  37. // Decrypt and check
  38. decoded, err := AESDecrypt(encoded, &k)
  39. Expect(err).ToNot(HaveOccurred())
  40. Expect(decoded).To(Equal(message))
  41. decoded, err = AESDecrypt(encoded2, &k)
  42. Expect(err).ToNot(HaveOccurred())
  43. Expect(decoded).To(Equal(message))
  44. })
  45. })
  46. })