Reputation: 163
I've been trying to generate secret key via rials but for some reasons stuff differs from what I see in python.
Python code:
# Import required Python libraries
import time
import base64
import hashlib
import hmac
# Decode API private key from base64 format displayed in account management
api_secret = base64.b64decode("FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ==")
# Variables (API method, nonce, and POST data)
api_path = "/0/private/TradeBalance"
api_nonce = str(int(time.time()*1000))
api_post = "nonce=" + api_nonce + "&asset=xbt"
# Cryptographic hash algorithms
api_sha256 = hashlib.sha256(api_nonce + api_post).digest()
api_hmac = hmac.new(api_secret, api_path + api_sha256, hashlib.sha512)
# Encode signature into base64 format used in API-Sign value
api_signature = base64.b64encode(api_hmac.digest())
# API authentication signature for use in API-Sign HTTP header
print(api_signature)
The closest I managed to get is this (Ruby on rails):
PRIVATE_KEY = "FRs+gtq09rR7OFtKj9BGhyOGS3u5vtY/EdiIBO9kD8NFtRX7w7LeJDSrX6cq1D8zmQmGkWFjksuhBvKOAWJohQ=="
secret = Base64.decode64(PRIVATE_KEY)
path = '/0/private/TradeBalance'
nonce = DateTime.now.iso8601(6).to_time.to_i
post = "nonce=" + nonce.to_s + '&asset=xbt'
sha256 = OpenSSL::Digest::SHA256.new(nonce.to_s + post)
hmac = OpenSSL::HMAC.new(secret, path + sha256.to_s)
But hmac returns:
Traceback (most recent call last):
3: from (irb):89
2: from (irb):89:in `new'
1: from (irb):89:in `initialize'
RuntimeError (Unsupported digest algorithm (/0/private/TradeBalance51967c001989cf328de93113f629f71b716be83fafb38dff63aef8d970d61df7).: first num too large)
I have also noticed that even my base64 decode differs from what python one returns in the first place.
Upvotes: 2
Views: 996
Reputation: 43
Ruby on Rails also provides a built in function like
SecureRandom.hex(64)
If you need a URL-safe key, say for API key generation, Ruby has you covered.
SecureRandom.urlsafe_base64
Upvotes: 3