def UseKey(purpose, loc, dest, crypter=None, dest2=None, loc2=None, crypter2=None, msg=None, param=[]): reader = readers.CreateReader(loc) reader2 = None if loc2: reader2 = readers.CreateReader(loc2) try: answer = "" answer2 = "" if crypter: reader = readers.EncryptedReader(reader, crypter) if crypter2 and reader2: reader2 = readers.EncryptedReader(reader2, crypter2) if purpose == "crypt": answer = keyczar.Encrypter(reader).Encrypt(msg) elif purpose == "sign": answer = keyczar.Signer(reader).Sign(msg) elif purpose == "sign-timeout": if len(param) > 0: expire_string = param[0] expire_date = datetime.datetime.strptime( expire_string, "%Y-%m-%dT%H:%M:%SZ") answer = keyczar.TimeoutSigner(reader).Sign(msg, expire_date) else: print "Needs UTC time as extra parameter for timeout expiration" elif purpose == "sign-attached": hidden = "" if len(param) > 0: hidden = param[0] answer = keyczar.Signer(reader).AttachedSign(msg, hidden) elif purpose == "sign-unversioned": answer = keyczar.UnversionedSigner(reader).Sign(msg) elif purpose == "crypt-session": sencrypt = keyczar.SessionEncrypter(keyczar.Encrypter(reader)) answer = sencrypt.session_material answer2 = sencrypt.Encrypt(msg) elif purpose == "crypt-signedsession": sencrypt = keyczar.SignedSessionEncrypter( keyczar.Encrypter(reader), keyczar.Signer(reader2)) answer = sencrypt.session_material answer2 = sencrypt.Encrypt(msg) util.WriteFile(answer, dest) if dest2: util.WriteFile(answer2, dest2) finally: reader.Close() if reader2: reader2.Close()
def testAesEncryptedKeyDecrypt(self): file_reader = readers.FileReader(os.path.join(TEST_DATA, "aes-crypted")) key_decrypter = keyczar.Crypter.Read(os.path.join(TEST_DATA, "aes")) reader = readers.EncryptedReader(file_reader, key_decrypter) self.__testDecrypt("aes-crypted", reader) self.__testDecryptReflowed("aes-crypted", reader) self.__testAllModesAndBufferSizes(self.__testDecryptStream, ("aes-crypted", reader,), no_data_reqd=True)
def UseKey(purpose, loc, dest, crypter=None, msg="This is some test data"): reader = readers.CreateReader(loc) try: answer = "" if crypter: reader = readers.EncryptedReader(reader, crypter) if purpose == keyinfo.DECRYPT_AND_ENCRYPT: answer = keyczar.Crypter(reader).Encrypt(msg) elif purpose == keyinfo.SIGN_AND_VERIFY: answer = keyczar.Signer(reader).Sign(msg) util.WriteFile(answer, dest) finally: reader.Close()
def CreateGenericKeyczar(loc, crypter=None): if mock is not None: return keyczar.GenericKeyczar(mock) if loc is None: raise errors.KeyczarError("Need location") else: generic = None reader = readers.CreateReader(loc) try: if crypter: reader = readers.EncryptedReader(reader, crypter) generic = keyczar.GenericKeyczar(reader) finally: reader.Close() return generic