Roy Kokkelkoren | 0659aac | 2015-10-25 16:12:11 +0100 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu> |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Yesudeep Mangalapilly | b5bab22 | 2011-08-24 16:53:39 +0530 | [diff] [blame] | 16 | '''Tests integer operations.''' |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 17 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 18 | import unittest2 |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 19 | |
Sybren A. Stüvel | 8c857f4 | 2011-07-30 20:39:02 +0200 | [diff] [blame] | 20 | import rsa.core |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 21 | |
Yesudeep Mangalapilly | aed6e86 | 2011-08-11 01:59:50 +0530 | [diff] [blame] | 22 | class IntegerTest(unittest2.TestCase): |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 23 | |
| 24 | def setUp(self): |
| 25 | (self.pub, self.priv) = rsa.newkeys(64) |
| 26 | |
| 27 | def test_enc_dec(self): |
| 28 | |
| 29 | message = 42 |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 30 | print("\tMessage: %d" % message) |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 31 | |
Sybren A. Stüvel | 8c857f4 | 2011-07-30 20:39:02 +0200 | [diff] [blame] | 32 | encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n) |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 33 | print("\tEncrypted: %d" % encrypted) |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 34 | |
Sybren A. Stüvel | 8c857f4 | 2011-07-30 20:39:02 +0200 | [diff] [blame] | 35 | decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n) |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 36 | print("\tDecrypted: %d" % decrypted) |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 37 | |
| 38 | self.assertEqual(message, decrypted) |
| 39 | |
| 40 | def test_sign_verify(self): |
| 41 | |
| 42 | message = 42 |
| 43 | |
Sybren A. Stüvel | 8c857f4 | 2011-07-30 20:39:02 +0200 | [diff] [blame] | 44 | signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n) |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 45 | print("\tSigned: %d" % signed) |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 46 | |
Sybren A. Stüvel | 8c857f4 | 2011-07-30 20:39:02 +0200 | [diff] [blame] | 47 | verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n) |
Yesudeep Mangalapilly | 5802431 | 2011-08-11 01:48:25 +0530 | [diff] [blame] | 48 | print("\tVerified: %d" % verified) |
Sybren A. Stüvel | ff3a1d0 | 2011-06-20 00:13:53 +0200 | [diff] [blame] | 49 | |
| 50 | self.assertEqual(message, verified) |
| 51 | |