test_integers.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  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. # https://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. """Tests integer operations."""
  15. import unittest
  16. import rsa
  17. import rsa.core
  18. class IntegerTest(unittest.TestCase):
  19. def setUp(self):
  20. (self.pub, self.priv) = rsa.newkeys(64)
  21. def test_enc_dec(self):
  22. message = 42
  23. print("\n\tMessage: %d" % message)
  24. encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
  25. print("\tEncrypted: %d" % encrypted)
  26. decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n)
  27. print("\tDecrypted: %d" % decrypted)
  28. self.assertEqual(message, decrypted)
  29. def test_sign_verify(self):
  30. message = 42
  31. signed = rsa.core.encrypt_int(message, self.priv.d, self.pub.n)
  32. print("\n\tSigned: %d" % signed)
  33. verified = rsa.core.decrypt_int(signed, self.pub.e, self.pub.n)
  34. print("\tVerified: %d" % verified)
  35. self.assertEqual(message, verified)