blob: 58af48c5d764e5493b8ca07a6ffa0ddae9fa508d [file] [log] [blame]
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01001#
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 Mangalapillyb5bab222011-08-24 16:53:39 +053016'''Tests integer operations.'''
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020017
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053018import unittest2
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020019
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020020import rsa.core
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020021
Yesudeep Mangalapillyaed6e862011-08-11 01:59:50 +053022class IntegerTest(unittest2.TestCase):
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020023
24 def setUp(self):
25 (self.pub, self.priv) = rsa.newkeys(64)
26
27 def test_enc_dec(self):
28
29 message = 42
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053030 print("\tMessage: %d" % message)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020031
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020032 encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053033 print("\tEncrypted: %d" % encrypted)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020034
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020035 decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053036 print("\tDecrypted: %d" % decrypted)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020037
38 self.assertEqual(message, decrypted)
39
40 def test_sign_verify(self):
41
42 message = 42
43
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020044 signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053045 print("\tSigned: %d" % signed)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020046
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020047 verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053048 print("\tVerified: %d" % verified)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020049
50 self.assertEqual(message, verified)
51