def sign_data(secret_key, data): data_str = data if isinstance(data, dict): data_str = json.dumps(data) data_bytes = bytes(data_str, 'utf-8') secret_key_bytes = binascii.unhexlify(secret_key) signed_data_bytes = crypto_sign(data_bytes, secret_key_bytes) return signed_data_bytes.hex()
def get_auth(user_id, name, secret, nonce, method, uri, body=None): if body is not None: if not isinstance(body, str): try: body = json.dumps(body, separators=(',', ':')) except ValueError as e: logger.exception( 'invalid body. json or string are valid body type') request_string = '{"method":"' + method + '","uri":"' + uri + ('",' if ( body is None) else '","body":' + body + ',') + '"nonce":' + nonce + '}' logger.debug("message %s", request_string) raw_signed = crypto_sign(request_string.encode(), bytes.fromhex(secret)) signature = Base64Encoder.encode(raw_signed[:crypto_sign_BYTES]) return 'SIGN ' + user_id + "." + name + ':' + signature.decode()
def test_sign(): seed = b"\x00" * c.crypto_sign_SEEDBYTES pubkey, secretkey = c.crypto_sign_seed_keypair(seed) assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES pubkey, secretkey = c.crypto_sign_keypair() assert len(pubkey) == c.crypto_sign_PUBLICKEYBYTES assert len(secretkey) == c.crypto_sign_SECRETKEYBYTES msg = b"message" sigmsg = c.crypto_sign(msg, secretkey) assert len(sigmsg) == len(msg) + c.crypto_sign_BYTES msg2 = c.crypto_sign_open(sigmsg, pubkey) assert msg2 == msg
def generate_headers(self, method: str, api_path: str, params: dict = None, body: dict = None) -> dict: nonce = str(round(datetime.now().timestamp())) string_to_sign = method + api_path string_to_sign = str(furl(string_to_sign).add(params)) if body: string_to_sign += json.dumps(body) string_to_sign += nonce signature_prefix = "dmar ed25519 " encoded = string_to_sign.encode('utf-8') secret_bytes = bytes.fromhex(self.SECRET_KEY) signature_bytes = crypto_sign(encoded, secret_bytes) signature = signature_bytes[:64].hex() headers = { "X-Api-Key": self.PUBLIC_KEY, "X-Request-Sign": signature_prefix + signature, "X-Sign-Date": nonce } return headers
def v2_sign(m, sk, f=EMPTY): h = V2_PUBLIC m2 = pae([h, m, f]) sig = sodium.crypto_sign(m2, sk)[:sodium.crypto_sign_BYTES] return _pack_msg(h, m + sig, f)
def crypto_sign(self, m, sk): return bindings.crypto_sign(m, sk)
"name": offer["title"], "image": offer["image"], "ownerGets": { "amount": "1", "currency": "USD" } } }] } nonce = str(round(datetime.now().timestamp())) api_url_path = "/exchange/v1/target/create" method = "POST" offer_from_market = get_offer_from_market() body = build_target_body_from_offer(offer_from_market) string_to_sign = method + api_url_path + json.dumps(body) + nonce signature_prefix = "dmar ed25519 " encoded = string_to_sign.encode('utf-8') secret_bytes = bytes.fromhex(secret_key) signature_bytes = crypto_sign(encoded, bytes.fromhex(secret_key)) signature = signature_bytes[:64].hex() headers = { "X-Api-Key": public_key, "X-Request-Sign": signature_prefix + signature, "X-Sign-Date": nonce } resp = requests.post(rootApiUrl + api_url_path, json=body, headers=headers) print(resp.text)