Skip to content

Instantly share code, notes, and snippets.

@jeiting
Created March 12, 2019 17:41
Show Gist options
  • Save jeiting/e47139e687bb62688eaf5efe34d9fe71 to your computer and use it in GitHub Desktop.
Save jeiting/e47139e687bb62688eaf5efe34d9fe71 to your computer and use it in GitHub Desktop.
import json
import uuid
import time
import hashlib
import base64
from ecdsa import SigningKey
from ecdsa.util import sigencode_der
bundle_id = 'com.myapp'
key_id = 'XWSXTGQVX2'
product = 'com.myapp.product.a'
offer = 'REFERENCE_CODE' # This is the code set in ASC
application_username = 'user_name' # Should be the same you use when
# making purchases
nonce = uuid.uuid4()
timestamp = int(round(time.time() * 1000))
payload = '\u2063'.join([bundle_id,
key_id,
product,
offer,
application_username,
str(nonce), # Should be lower case
str(timestamp)])
# Read the key file
with open('cert.der', 'rb') as myfile:
der = myfile.read()
signing_key = SigningKey.from_der(der)
signature = signing_key.sign(payload.encode('utf-8'),
hashfunc=hashlib.sha256,
sigencode=sigencode_der)
encoded_signature = base64.b64encode(signature)
print(str(encoded_signature, 'utf-8'), str(nonce), str(timestamp), key_id)
@arifulalam
Copy link

Hi :),
Is this possible to generate this signature using PHP instead of Python?

Regards

@alexcc4
Copy link

alexcc4 commented May 24, 2019

Hi, I just try, not working.It seems invalid signature.Any update?

@xiongjinpeng
Copy link

xiongjinpeng commented Feb 23, 2022

payload.encode('utf-8') maybe modify to payload.decode("unicode_escape").encode('utf-8') ?

@pnico
Copy link

pnico commented May 10, 2022

payload.encode('utf-8') maybe modify to payload.decode("unicode_escape").encode('utf-8') ?

@xiongjinpeng I owe you a beer ❤️ why is this needed? I don't care, it works now 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment