test_integers.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Tests integer operations."""
  17. import unittest
  18. import rsa
  19. import rsa.core
  20. class IntegerTest(unittest.TestCase):
  21. def setUp(self):
  22. (self.pub, self.priv) = rsa.newkeys(64)
  23. def test_enc_dec(self):
  24. message = 42
  25. print("\tMessage: %d" % message)
  26. encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
  27. print("\tEncrypted: %d" % encrypted)
  28. decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n)
  29. print("\tDecrypted: %d" % decrypted)
  30. self.assertEqual(message, decrypted)
  31. def test_sign_verify(self):
  32. message = 42
  33. signed = rsa.core.encrypt_int(message, self.priv.d, self.pub.n)
  34. print("\tSigned: %d" % signed)
  35. verified = rsa.core.decrypt_int(signed, self.pub.e, self.pub.n)
  36. print("\tVerified: %d" % verified)
  37. self.assertEqual(message, verified)