I have a Python script to create signature/token for API requests. It works fine.
I have written almost all lines from Python to Bash. Now I am not able to decode/translate below lines from Python to Bash so that correct signature gets created and I complete my bash script.
from Crypto.PublicKey import RSA
from Crypto.Signature import pss
from Crypto.Hash import SHA256
# Load private key
try:
with open(private_key_location, 'r') as file:
# Read the contents of the file
private_key_string = file.read()
# Load private key
private_key = RSA.import_key(
private_key_string, passphrase=passphrase if passphrase else None)
# Sign
h = SHA256.new(signing_string.encode('utf-8'))
signature = pss.new(private_key).sign(h)
# Encode
encoded_signature = base64encode(signature)
First thing I get stuck is this line signing_string.encode('utf-8')
as in Python it will create a byte string b'
. I tried iconv in bash for byte string but doesn’t seem to fill the purpose. But I may be wrong.
Second thing I tried to sign and all is using below command in bash.
echo -n $MESSAGE |
openssl dgst -sha256 -hmac $SECRET -binary
base64
This outputs a few characters but when I run the Python one it gives a lot of characters wrapped over multiple lines.
I tried the below too:
echo $MESSAGE |
openssl dgst -sha256 -sign private.pem |
base64
This gives a lot of characters wrapped in multiple lines like the Python one, but doesn’t generates the correct signature for server to authorize, so logic problem.
I need help in translating Python code above to Bash to be able to generate signature.
I know I can go ahead and use this whole code in Bash using python -c ""
command etc, etc, but my goal is pure Bash script.
5