-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some preparations for the next flare-on write-up
- Loading branch information
1 parent
050ab04
commit eea6a1a
Showing
12 changed files
with
7,909 additions
and
8,072 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Optional, Dict | ||
|
||
import hashlib | ||
import os | ||
import urllib.request | ||
|
||
from refinery.units.crypto.cipher.aes import aes | ||
|
||
|
||
class SampleStore: | ||
cache: Dict[str, bytes] | ||
|
||
def __init__(self): | ||
self.cache = {} | ||
|
||
def download(self, sha256hash: str, key: Optional[str] = None): | ||
def tobytearray(r): | ||
if isinstance(r, bytearray): | ||
return r | ||
return bytearray(r) | ||
key = key or 'REFINERYTESTDATA' | ||
key = key.encode('latin1') | ||
sha256hash = sha256hash.lower() | ||
req = urllib.request.Request( | ||
F'https://github.com/binref/refinery-test-data/blob/master/{sha256hash}.enc?raw=true') | ||
try: | ||
with urllib.request.urlopen(req) as response: | ||
encoded_sample = tobytearray(response.read()) | ||
except Exception: | ||
api = os.environ['MALSHARE_API'] | ||
req = urllib.request.Request( | ||
F'https://malshare.com/api.php?api_key={api}&action=getfile&hash={sha256hash}') | ||
with urllib.request.urlopen(req) as response: | ||
result = tobytearray(response.read()) | ||
else: | ||
result = encoded_sample | aes(mode='CBC', key=key) | bytearray | ||
if not result or hashlib.sha256(result).hexdigest().lower() != sha256hash: | ||
raise ValueError('sample did not decode correctly') | ||
self.cache[sha256hash] = result | ||
return result | ||
|
||
def get(self, sha256hash: str, key: Optional[str] = None): | ||
for cached, value in self.cache.items(): | ||
if cached.casefold() == sha256hash.casefold(): | ||
return value | ||
else: | ||
return self.download(sha256hash, key) | ||
|
||
def __getitem__(self, sha256hash: str): | ||
return self.get(sha256hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Strips the Jupyter Notebooks in the Tutorial section of run count information. | ||
""" | ||
import json | ||
import pathlib | ||
|
||
for path in pathlib.Path.cwd().glob('./tutorials/*.ipynb'): | ||
with path.open('r') as fd: | ||
notebook = json.load(fd) | ||
for cell in notebook['cells']: | ||
cell.pop('execution_count', None) | ||
with path.open('w') as fd: | ||
json.dump(notebook, fd, indent=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.