def get_by_filename(cls, filename): data_fetched = False configuration = Configuration() binary_cache_path = os.path.join(configuration.get_entry("global.binary_cache"), configuration.get_entry("core.instance_id")) if not os.path.exists(binary_cache_path): os.mkdir(binary_cache_path,True) db = Database() if os.path.exists(os.path.join(binary_cache_path, filename)): cachefile = open(os.path.join(binary_cache_path, filename),"rb") data = cachefile.read() cachefile.close() md5 = md5hash(data).hexdigest() sha256 = sha256hash(data).hexdigest() stmnt = "SELECT BIN_ID, BIN_MIME, \ (SELECT BIN_DATA FROM BINARIES WHERE BIN_FILENAME = ? AND BIN_MD5 != ? AND BIN_SHA256 != ?) AS BIN_DATA \ FROM BINARIES WHERE BIN_FILENAME = ? ;" cur = db.query(stmnt, (filename, md5, sha256, filename)) row = cur.fetchonemap() if row is not None: data_fetched = True if row["BIN_DATA"] is None: bin = Binary() bin.set_id(row["BIN_ID"]) bin.set_filename(filename) bin.set_mime(row["BIN_MIME"]) bin.set_data(data) return bin else: raise BinaryException(BinaryException.get_msg(0, filename)) if not data_fetched: stmnt = "SELECT BIN_ID, BIN_MIME, BIN_DATA FROM BINARIES WHERE BIN_FILENAME = ? ;" cur = db.query(stmnt, (filename,)) row = cur.fetchonemap() if row is not None: bin = Binary() bin.set_id(row["BIN_ID"]) bin.set_filename(filename) bin.set_mime(row["BIN_MIME"]) bin.set_data(base64.b64decode(row["BIN_DATA"])) cachefile = open(os.path.join(binary_cache_path, filename),"wb") cachefile.write(bin.get_data()) cachefile.close() return bin else: raise BinaryException(BinaryException.get_msg(0, filename))
def store(self): """ Stores this binary into the database """ db = Database() data_io = base64.b64encode(self._data) md5 = md5hash(self._data).hexdigest() sha256 = sha256hash(self._data).hexdigest() if self._id is None: self.set_id(db.get_seq_next("BIN_GEN")) user_id = Session.get_current_session_user().get_id() stmnt = "UPDATE OR INSERT INTO BINARIES (BIN_ID, BIN_FILENAME, BIN_MIME, BIN_USR_OWNER, \ BIN_USR_LASTCHANGE, \ BIN_DATE_LASTCHANGE, BIN_SHA256, BIN_MD5, BIN_DATA) \ VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?, ? ) MATCHING (BIN_ID) ;" db.query(stmnt, (self._id, self._filename, self._mime, user_id, user_id, sha256, md5, data_io),commit=True)
def store(self): """ Stores this binary into the database """ db = self._core.get_db() data_io = StringIO(base64.b64encode(self._data)) md5 = md5hash(self._data).hexdigest() sha256 = sha256hash(self._data).hexdigest() if self._id is None: self.set_id(db.get_seq_next("BIN_GEN")) user_id = self._core.get_session_manager().get_current_session_user( ).get_id() stmnt = "UPDATE OR INSERT INTO BINARIES (BIN_ID, BIN_FILENAME, BIN_MIME, BIN_USR_OWNER, \ BIN_USR_LASTCHANGE, \ BIN_DATE_LASTCHANGE, BIN_SHA256, BIN_MD5, BIN_DATA) \ VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?, ? ) MATCHING (BIN_ID) ;" db.query(self._core, stmnt, (self._id, self._filename, self._mime, user_id, user_id, sha256, md5, data_io), commit=True) data_io.close()
def password_hash(pw): val = "".encode('utf-8') pw = pw.encode('utf-8') for _ in range(50000): val = sha256hash(val + pw).digest() return hex(val).decode('utf-8')