def __init__( self, CreatorID=None, BackupID='', BlockNumber=0, SessionKey='', SessionKeyType=None, LastBlock=True, Data='', EncryptKey=None, DecryptKey=None, ): self.CreatorID = CreatorID if not self.CreatorID: self.CreatorID = my_id.getLocalID() self.BackupID = str(BackupID) self.BlockNumber = BlockNumber if callable(EncryptKey): self.EncryptedSessionKey = EncryptKey(SessionKey) elif isinstance(EncryptKey, basestring): self.EncryptedSessionKey = my_keys.encrypt(EncryptKey, SessionKey) else: self.EncryptedSessionKey = key.EncryptLocalPublicKey(SessionKey) self.SessionKeyType = SessionKeyType if not self.SessionKeyType: self.SessionKeyType = key.SessionKeyType() self.Length = len(Data) self.LastBlock = bool(LastBlock) self.EncryptedData = key.EncryptWithSessionKey(SessionKey, Data) # DataLonger self.Signature = None self.Sign() self.DecryptKey = DecryptKey if _Debug: lg.out(_DebugLevel, 'new data in %s' % self)
def encrypt(self, message_body, encrypt_session_func=None): if _Debug: lg.args(_DebugLevel, encrypt_session_func=encrypt_session_func, recipient=self.recipient) new_sessionkey = key.NewSessionKey(session_key_type=key.SessionKeyType()) if not encrypt_session_func: if my_keys.is_key_registered(self.recipient): if _Debug: lg.dbg(_DebugLevel, 'with registered key %r' % self.recipient) encrypt_session_func = lambda inp: my_keys.encrypt(self.recipient, inp) if not encrypt_session_func: glob_id = global_id.NormalizeGlobalID(self.recipient) if glob_id['key_alias'] == 'master': if glob_id['idurl'] == my_id.getIDURL(): lg.warn('making encrypted message addressed to me ?') encrypt_session_func = lambda inp: my_keys.encrypt('master', inp) else: remote_identity = identitycache.FromCache(glob_id['idurl']) if not remote_identity: raise Exception('remote identity is not cached yet, not able to encrypt the message') if _Debug: lg.dbg(_DebugLevel, 'with remote identity public key %r' % glob_id['idurl']) encrypt_session_func = remote_identity.encrypt else: own_key = global_id.MakeGlobalID(idurl=my_id.getIDURL(), key_alias=glob_id['key_alias']) if my_keys.is_key_registered(own_key): if _Debug: lg.dbg(_DebugLevel, 'with registered key (found by alias) %r' % own_key) encrypt_session_func = lambda inp: my_keys.encrypt(own_key, inp) if not encrypt_session_func: raise Exception('can not find key for given recipient') self.encrypted_session = encrypt_session_func(new_sessionkey) self.encrypted_body = key.EncryptWithSessionKey(new_sessionkey, message_body, session_key_type=key.SessionKeyType()) return self.encrypted_session, self.encrypted_body
def encrypt(self, message_body, encrypt_session_func=None): new_sessionkey = key.NewSessionKey() if not encrypt_session_func: if my_keys.is_key_registered(self.recipient): if _Debug: lg.out(_DebugLevel, 'message.PrivateMessage.encrypt with "%s" key' % self.recipient) encrypt_session_func = lambda inp: my_keys.encrypt(self.recipient, inp) if not encrypt_session_func: glob_id = global_id.ParseGlobalID(self.recipient) if glob_id['key_alias'] == 'master': if glob_id['idurl'] == my_id.getLocalID(): lg.warn('making private message addressed to me ???') if _Debug: lg.out(_DebugLevel, 'message.PrivateMessage.encrypt with "master" key') encrypt_session_func = lambda inp: my_keys.encrypt('master', inp) else: remote_identity = identitycache.FromCache(glob_id['idurl']) if not remote_identity: raise Exception('remote identity is not cached yet, not able to encrypt the message') if _Debug: lg.out(_DebugLevel, 'message.PrivateMessage.encrypt with remote identity public key') encrypt_session_func = remote_identity.encrypt else: own_key = global_id.MakeGlobalID(idurl=my_id.getLocalID(), key_alias=glob_id['key_alias']) if my_keys.is_key_registered(own_key): if _Debug: lg.out(_DebugLevel, 'message.PrivateMessage.encrypt with "%s" key' % own_key) encrypt_session_func = lambda inp: my_keys.encrypt(own_key, inp) if not encrypt_session_func: raise Exception('can not find key for given recipient') self.encrypted_session = encrypt_session_func(new_sessionkey) self.encrypted_body = key.EncryptWithSessionKey(new_sessionkey, message_body) return self.encrypted_session, self.encrypted_body
def __init__( self, CreatorID=None, BackupID='', BlockNumber=0, SessionKey='', SessionKeyType=None, LastBlock=True, Data=b'', EncryptKey=None, DecryptKey=None, EncryptedSessionKey=None, EncryptedData=None, Length=None, Signature=None, ): self.CreatorID = CreatorID if not self.CreatorID: self.CreatorID = my_id.getLocalID() if not isinstance(self.CreatorID, id_url.ID_URL_FIELD): self.CreatorID = id_url.field(self.CreatorID) self.BackupID = strng.to_text(BackupID) self.BlockNumber = BlockNumber self.LastBlock = bool(LastBlock) self.SessionKeyType = SessionKeyType or key.SessionKeyType() if EncryptedSessionKey: # this block to be decrypted after receiving self.EncryptedSessionKey = EncryptedSessionKey else: # this block to be encrypted before sending if callable(EncryptKey): self.EncryptedSessionKey = EncryptKey(SessionKey) elif strng.is_text(EncryptKey): self.EncryptedSessionKey = my_keys.encrypt( EncryptKey, SessionKey) elif strng.is_bin(EncryptKey): self.EncryptedSessionKey = my_keys.encrypt( strng.to_text(EncryptKey), SessionKey) else: self.EncryptedSessionKey = key.EncryptLocalPublicKey( SessionKey) if EncryptedData and Length is not None: self.Length = Length self.EncryptedData = EncryptedData else: self.Length = len(Data) self.EncryptedData = key.EncryptWithSessionKey( SessionKey, Data, session_key_type=self.SessionKeyType) if Signature: self.Signature = Signature else: self.Signature = None self.Sign(signing_key=EncryptKey) self.DecryptKey = DecryptKey if _Debug: lg.out(_DebugLevel, 'new data in %s' % self)
def __init__( self, CreatorID=None, BackupID='', BlockNumber=0, SessionKey='', SessionKeyType=None, LastBlock=True, Data='', EncryptKey=None, DecryptKey=None, EncryptedSessionKey=None, EncryptedData=None, Length=None, Signature=None, ): self.CreatorID = CreatorID if not self.CreatorID: self.CreatorID = my_id.getLocalID() self.CreatorID = strng.to_bin(self.CreatorID) self.BackupID = strng.to_text(BackupID) self.BlockNumber = BlockNumber self.LastBlock = bool(LastBlock) self.SessionKeyType = SessionKeyType or key.SessionKeyType() if EncryptedSessionKey: self.EncryptedSessionKey = EncryptedSessionKey else: if callable(EncryptKey): self.EncryptedSessionKey = EncryptKey(SessionKey) elif isinstance(EncryptKey, six.string_types): self.EncryptedSessionKey = my_keys.encrypt( EncryptKey, SessionKey) else: self.EncryptedSessionKey = key.EncryptLocalPublicKey( SessionKey) if EncryptedData and Length: self.Length = Length self.EncryptedData = EncryptedData else: self.Length = len(Data) self.EncryptedData = key.EncryptWithSessionKey(SessionKey, Data) if Signature: self.Signature = Signature else: self.Signature = None self.Sign() self.DecryptKey = DecryptKey if _Debug: lg.out(_DebugLevel, 'new data in %s' % self)