class CIDTestCase(object): def test_cidv0_eq_cidv0(self, test_hash): """ check for equality for CIDv0 for same hash """ assert CIDv0(test_hash) == make_cid(CIDv0(test_hash).encode()) def test_cidv0_neq(self): """ check for inequality for CIDv0 for different hashes """ assert CIDv0(b'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') != \ CIDv0(b'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1o') def test_cidv0_eq_cidv1(self, test_hash): """ check for equality between converted v0 to v1 """ assert CIDv0(test_hash).to_v1() == CIDv1(CIDv0.CODEC, test_hash) def test_cidv1_eq_cidv0(self): """ check for equality between converted v1 to v0 """ assert CIDv1(CIDv0.CODEC, test_hash).to_v0() == CIDv0(test_hash) def test_cidv1_to_cidv0_no_dag_pb(self): """ converting non dag-pb CIDv1 should raise an exception """ with pytest.raises(ValueError) as excinfo: CIDv1('base2', test_hash).to_v0() assert 'can only be converted for codec' in str(excinfo.value) def test_is_cid_valid(self, test_hash): assert is_cid(CIDv0(test_hash).encode()) @pytest.mark.parametrize('test_data', ( 'foobar', b'foobar', multibase.encode('base58btc', b'foobar'), )) def test_is_cid_invalid(self, test_data): assert not is_cid(test_data)
def encode(data: str) -> str: assert_type(data, str) hash_object = hashlib.sha256() hash_object.update(bytes(data, "utf-8")) multi = multihash.encode(hash_object.digest(), "sha2-256") result = multibase.encode("base58btc", multi) return result.decode("utf-8")
def encode(self, encoding='base58btc'): """ Encoded version of the raw representation :param str encoding: the encoding to use to encode the raw representation, should be supported by ``py-multibase`` :return: encoded raw representation with the given encoding :rtype: bytes """ return multibase.encode(encoding, self.buffer)
class CIDTestCase(object): def test_cidv0_eq_cidv0(self, test_hash): """ check for equality for CIDv0 for same hash """ assert CIDv0(test_hash) == make_cid(CIDv0(test_hash).encode()) def test_cidv0_neq(self): """ check for inequality for CIDv0 for different hashes """ assert CIDv0(b'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') != \ CIDv0(b'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1o') def test_cidv0_eq_cidv1(self, test_hash): """ check for equality between converted v0 to v1 """ assert CIDv0(test_hash).to_v1() == CIDv1(CIDv0.CODEC, test_hash) def test_cidv1_eq_cidv0(self, test_hash): """ check for equality between converted v1 to v0 """ assert CIDv1(CIDv0.CODEC, test_hash).to_v0() == CIDv0(test_hash) def test_cidv1_to_cidv0_no_dag_pb(self): """ converting non dag-pb CIDv1 should raise an exception """ with pytest.raises(ValueError) as excinfo: CIDv1('base2', test_hash).to_v0() assert 'can only be converted for codec' in str(excinfo.value) @pytest.mark.parametrize('test_cidv0', ('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR', 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') ) def test_is_cidv0_valid(self, test_cidv0): assert is_cid(test_cidv0) assert is_cid(make_cid(test_cidv0).encode()) @given(hash=st.text(string.ascii_letters + string.digits)) def test_make_cid(self, hash): is_cid(hash) @pytest.mark.parametrize( 'test_cidv1', ('bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi', 'bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy', 'zb2rhk6GMPQF3hfzwXTaNYFLKomMeC6UXdUt6jZKPpeVirLtV')) def test_is_cidv1_valid(self, test_cidv1): assert is_cid(test_cidv1) assert is_cid(make_cid(test_cidv1).encode()) @pytest.mark.parametrize('test_data', ( '!', 'a', '1', 'foobar', b'foobar', multibase.encode('base58btc', b'foobar'), )) def test_is_cid_invalid(self, test_data): assert not is_cid(test_data)
def generate_key(received_args: Optional[CLIargs] = None) -> str: if received_args is None: received_args = command_line_arguments[1:] args = parse_args(received_args) check_path(args.image_path) image: Array = imread(args.image_path) digest = hash_image_pixels(image, sha3_256) b58digest: bytes = encode("base58btc", digest) # TODO: rotated images are generating different keys. Is that ok? h, w, c = image.shape # TODO: use multihash return f"pixelwise-{h}-{w}-{c}--sha3_256--{b58digest.decode('utf-8')}"
def create_cert_request(data): crt = CrtRequest() crt.issuedOn = int(datetime.now().timestamp()) * 1000 crt.status = 'PENDING' crt.payload = data cid = multibase.encode('base58btc', json.dumps((crt))) crt.cid = cid.decode("utf-8") crt.save() thread = providerThread('xxx', 'caProvider', crt) thread.start() return True, crt
def encode(self, proof_json): if type(proof_json) == str: self.json = json.loads(proof_json) else: self.json = proof_json # map json to cbor-efficient values mapped_values = self.__map_json() # cbor encode map cbor_encoding = dumps(mapped_values) # multibase encode to base58btc self.proof_value = encode('base58btc', cbor_encoding) return self.proof_value
def mbencode(base, s): # # We use base58 because py-multibase is known to fail cases with leading zeros # # See: # - https://github.com/multiformats/py-multibase/issues/11 # - https://github.com/multiformats/multibase/issues/34 # if base == "base58btc": return b"z" + base58.b58encode(s) if base == "base64pad": return b"M" + base64.b64encode(s) if base == "base64urlpad": return b"U" + base64.urlsafe_b64encode(s) return multibase.encode(base, s)
def generate_digest(url): if url is None or url == "": return "" with urllib.request.urlopen(url) as response: with tempfile.NamedTemporaryFile(delete=False) as tmp_file: shutil.copyfileobj(response, tmp_file) sha256_hash = hashlib.sha256() # Read and update hash string value in blocks of 4K for byte_block in iter(lambda: tmp_file.file.read(4096), b""): sha256_hash.update(byte_block) hex_digest = sha256_hash.hexdigest() multi_codec = add_prefix('sha2-256', str.encode(hex_digest)) base58btc = encode('base58btc', multi_codec) digest = base58btc.decode() return digest
def _calculate_hash(self): '''hash over the content of this file use multihash to self-describe the hash used (blake2b-16) use multibase to self-describe the encoding (b32) ''' # this takes just under 2s on my laptop for a 1GB file. b2 = blake2b(digest_size=16) current = self._fd.tell() self._fd.seek(current) while True: data = self._fd.read(self._HASH_BUF_SIZE) if not data: break b2.update(data) self._fd.seek(current) mh = multihash.encode(b2.digest(), 'blake2b-16') return multibase.encode('base32', mh)
def run(self): print("Starting CA Default " + self.name) # process_data(self.name, self.q) certificate = DefaultCertificate() certificate.certKind = "A" certificate.issuedOn = int(datetime.now().timestamp()) * 1000 certificate.authorizedUntil = 1596769200000 certificate.issuedOn = 1588905600973 certificate.typeOfCertificate = 'PERMISO DE TRABAJO' certificate.authorizedSince = 1591498800000 certificate.status = 'APPROVED' cid = multibase.encode('base58btc', json.dumps(certificate)).decode("utf-8") certificate.cid = str(cid) certificate.save() self.crt.status = "RESOLVED" self.crt.resolvedOn = int(datetime.now().timestamp()) * 1000 self.crt.save() print("Exiting CA Default " + self.name)
def test_encode_incorrect_encoding(encoding): with pytest.raises(ValueError) as excinfo: encode(encoding, 'test data') assert 'not supported' in str(excinfo.value)
def test_encode(encoding, data, encoded_data): assert encode(encoding, data) == ensure_bytes(encoded_data)
def test_encode_encoding(self, cid, codec): """ #encode uses the encoding provided for encoding """ assert cid.encode(codec.encoding) == multibase.encode(codec.encoding, cid.buffer)
def b16compage(a, b): a_base16ascii = mb.encode('base16', a).decode('ascii') b_base16ascii = mb.encode('base16', b).decode('ascii') return b_base16ascii == a_base16ascii
#!/usr/bin/env python3 from stbx_hash import encode, decode, hash # buf = bytes.fromhex('deadbeeffeedcafebabec0de') buf = bytes.fromhex('446563656e7472616c697a652065766572797468696e672121') s = encode(buf) import multibase as mb print(mb.encode('base16', buf).decode('ascii')) print(mb.encode('base32', buf).decode('ascii')) print(decode(s)) print(decode(s).hex()) print(mb.encode('base32', s).decode('ascii')) print(s) def b16compage(a, b): a_base16ascii = mb.encode('base16', a).decode('ascii') b_base16ascii = mb.encode('base16', b).decode('ascii') return b_base16ascii == a_base16ascii def decode_over_encode(buf): a = buf b = decode(encode(a)) return b16compage(a,b) assert(decode_over_encode(buf)) assert(decode_over_encode(bytes())) assert(decode_over_encode(bytes([]))) assert(decode_over_encode(bytes([1]))) assert(decode_over_encode(bytes([255]))) assert(decode_over_encode(bytes([255,255])))