blob: 118204aeeb5248d673ac7c12a629e66426f11dc9 [file] [log] [blame]
Sybren A. Stüvel9d2cd9a2015-11-05 19:53:38 +00001# -*- coding: utf-8 -*-
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01002#
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# http://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
Yesudeep Mangalapillyb5bab222011-08-24 16:53:39 +053017'''Tests integer operations.'''
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020018
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010019import unittest
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020020
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020021import rsa.core
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020022
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010023class IntegerTest(unittest.TestCase):
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020024
25 def setUp(self):
26 (self.pub, self.priv) = rsa.newkeys(64)
27
28 def test_enc_dec(self):
29
30 message = 42
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053031 print("\tMessage: %d" % message)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020032
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020033 encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053034 print("\tEncrypted: %d" % encrypted)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020035
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020036 decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053037 print("\tDecrypted: %d" % decrypted)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020038
39 self.assertEqual(message, decrypted)
40
41 def test_sign_verify(self):
42
43 message = 42
44
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020045 signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053046 print("\tSigned: %d" % signed)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020047
Sybren A. Stüvel8c857f42011-07-30 20:39:02 +020048 verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n)
Yesudeep Mangalapilly58024312011-08-11 01:48:25 +053049 print("\tVerified: %d" % verified)
Sybren A. Stüvelff3a1d02011-06-20 00:13:53 +020050
51 self.assertEqual(message, verified)
52