def test_multiple_signings(self): '''Signing the same message twice should return the same signatures.''' message = struct.pack('>IIII', 0, 0, 0, 1) signature1 = pkcs1.sign(message, self.priv, 'SHA-1') signature2 = pkcs1.sign(message, self.priv, 'SHA-1') self.assertEqual(signature1, signature2)
def test_multiple_signings(self): """Signing the same message twice should return the same signatures.""" message = struct.pack('>IIII', 0, 0, 0, 1) signature1 = pkcs1.sign(message, self.priv, 'SHA-1') signature2 = pkcs1.sign(message, self.priv, 'SHA-1') self.assertEqual(signature1, signature2)
def test_sign_verify(self): """Test happy flow of sign and verify""" message = b'je moeder' signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertEqual('SHA-256', pkcs1.verify(message, signature, self.pub)) signature = pkcs1.sign(message, self.priv, 'SHA3-256') self.assertEqual('SHA3-256', pkcs1.verify(message, signature, self.pub))
def test_sign_verify_sha3(self): """Test happy flow of sign and verify with SHA3-256""" message = b"je moeder" signature = pkcs1.sign(message, self.priv, "SHA3-256") self.assertEqual("SHA3-256", pkcs1.verify(message, signature, self.pub))
def sign(self, data): """ hashes the data with sha1 and signs the result using pkcs1.5 and SHA-1 returns bytes data must be bytes! """ return pkcs1.sign(data, self.privkey, 'SHA-1')
def test_find_signature_hash(self): """Test happy flow of sign and find_signature_hash""" message = b'je moeder' signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertEqual('SHA-256', pkcs1.find_signature_hash(signature, self.pub))
def test_apppend_zeroes(self): """Apppending the signature with zeroes should be detected.""" message = b'je moeder' signature = pkcs1.sign(message, self.priv, 'SHA-256') signature = signature + bytes.fromhex('0000') with self.assertRaises(rsa.VerificationError): pkcs1.verify(message, signature, self.pub)
def test_prepend_zeroes(self): """Prepending the signature with zeroes should be detected.""" message = b'je moeder' signature = pkcs1.sign(message, self.priv, 'SHA-256') signature = b'\00\00' + signature with self.assertRaises(rsa.VerificationError): pkcs1.verify(message, signature, self.pub)
def test_find_signature_hash(self): """Test happy flow of sign and find_signature_hash""" message = b"je moeder" signature = pkcs1.sign(message, self.priv, "SHA-256") self.assertEqual("SHA-256", pkcs1.find_signature_hash(signature, self.pub))
def test_prepend_zeroes(self): """Prepending the signature with zeroes should be detected.""" message = b"je moeder" signature = pkcs1.sign(message, self.priv, "SHA-256") signature = bytes.fromhex("0000") + signature with self.assertRaises(rsa.VerificationError): pkcs1.verify(message, signature, self.pub)
def test_sign_different_key(self): """Signing with another key should let the verification fail.""" (otherpub, _) = rsa.newkeys(512) message = b('je moeder') signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, message, signature, otherpub)
def test_sign_different_key(self): '''Signing with another key should let the verification fail.''' (otherpub, _) = rsa.newkeys(512) message = b('je moeder') signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, message, signature, otherpub)
def test_sign_verify(self): """Test happy flow of sign and verify""" message = b('je moeder') print("\tMessage: %r" % message) signature = pkcs1.sign(message, self.priv, 'SHA-256') print("\tSignature: %r" % signature) self.assertTrue(pkcs1.verify(message, signature, self.pub))
def test_sign_verify(self): '''Test happy flow of sign and verify''' message = b('je moeder') print("\tMessage: %r" % message) signature = pkcs1.sign(message, self.priv, 'SHA-256') print("\tSignature: %r" % signature) self.assertTrue(pkcs1.verify(message, signature, self.pub))
def test_sign_verify(self): '''Test happy flow of sign and verify''' message = 'je moeder' print "\tMessage: %r" % message signature = pkcs1.sign(message, self.priv, 'SHA-256') print "\tSignature: %r" % signature pkcs1.verify(message, signature, self.pub)
def test_sign_verify(self): '''Test happy flow of sign and verify''' message = b('je moeder') print("\tMessage: %r" % message) signature = pkcs1.sign(message, self.priv, 'SHA-256') print("\tSignature: %r" % signature) pkcs1.verify(message, signature, self.pub)
def test_split_hash_sign(self): """Hashing and then signing should match with directly signing the message. """ message = b'je moeder' msg_hash = pkcs1.compute_hash(message, 'SHA-256') signature1 = pkcs1.sign_hash(msg_hash, self.priv, 'SHA-256') # Calculate the signature using the unified method signature2 = pkcs1.sign(message, self.priv, 'SHA-256') self.assertEqual(signature1, signature2)
def test_sign_verify_bigfile(self): # Large enough to store MD5-sum and ASN.1 code for MD5 pub_key, priv_key = rsa.newkeys((34 + 11) * 8) # Sign the file msgfile = BytesIO(b("123456Sybren")) signature = pkcs1.sign(msgfile, priv_key, "MD5") # Check the signature msgfile.seek(0) self.assertTrue(pkcs1.verify(msgfile, signature, pub_key)) # Alter the message, re-check msgfile = BytesIO(b("123456sybren")) self.assertRaises(pkcs1.VerificationError, pkcs1.verify, msgfile, signature, pub_key)
def test_sign_verify_bigfile(self): # Large enough to store MD5-sum and ASN.1 code for MD5 pub_key, priv_key = rsa.newkeys((34 + 11) * 8) # Sign the file msgfile = BytesIO(b('123456Sybren')) signature = pkcs1.sign(msgfile, priv_key, 'MD5') # Check the signature msgfile.seek(0) self.assertTrue(pkcs1.verify(msgfile, signature, pub_key)) # Alter the message, re-check msgfile = BytesIO(b('123456sybren')) self.assertRaises(pkcs1.VerificationError, pkcs1.verify, msgfile, signature, pub_key)
def build(self): print('jwt: build') time = ntp.time() print('jwt: time: %d' % time) self._claim['iat'] = time self._claim['exp'] = time + self._expiration encoded_header = encode_dict_to_base64(self._header) encoded_claim = encode_dict_to_base64(self._claim) to_be_signed = '%s.%s' % (encoded_header, encoded_claim) print('jwt: signing ...') signature = pkcs1.sign(to_be_signed.encode('utf8'), self._key, 'SHA-256') print('jwt: done') print('jwt: encoding') encoded_signature = encode_bytes_to_safe_base64(signature) return '%s.%s' % (to_be_signed, encoded_signature)
def test_alter_message(self): """Altering the message should let the verification fail.""" signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, b('mijn moeder'), signature, self.pub)
def test_alter_message(self): '''Altering the message should let the verification fail.''' signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, b('mijn moeder'), signature, self.pub)
def sign(self, message): message = _helpers.to_bytes(message) import rsa.pkcs1 as pkcs1 return pkcs1.sign(message, self._key, "SHA-256")
""" return pkcs1.sign(data, self.privkey, 'SHA-1') class OpenSSLRSAPrivateKey: def __init__(self): self.p = None self.q = None self.e = None with open('ssh_server_test_cert.priv', 'rb') as f: type_name, headers, der_bytes = pem.unarmor(f.read()) object = keys.RSAPrivateKey.load(der_bytes) print(object['modulus'].native) print(object.native) with open('ssh_server_test_cert.priv', 'rb') as f: keydata = f.read() privkey = rsa.PrivateKey.load_pkcs1(keydata, 'PEM') print(privkey) a = pkcs1.sign('alma'.encode(), privkey, 'SHA-1') pk = SSHPrivKey.load_privkey_from_file('ssh_server_test_cert.priv') print() print(pk.sign('alma'.encode())) #sig = self.key.sign( # data, padding=padding.PKCS1v15(), algorithm=hashes.SHA1() #)