Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
digest header generating issue
#1
Hello,
I'm trying to generate digest header for the request similarly to JavaScript code from example, however python script produces different result

This is JavaScript code:

import sha256 from "crypto-js/sha256";
import Base64 from "crypto-js/enc-base64";

// Test payload to compare python and js output
const payload = {
    code: "this_is_fake_code",
    grant_type: "authorization_code",
    redirect_uri: "this_is_redirect_url",
};

function calculateDigest() {
    const payload_data = JSON.stringify(payload);
    const sha256digest = sha256(payload_data);
    const base64sha256 = Base64.stringify(sha256digest);
    const calculatedDigest = "sha-256=" + base64sha256;
    return calculatedDigest;
}

calculateDigest();
Origin of JavaScript code Nordea API ; function name in doc is also calculateDigest
It produces digest: sha-256=wAbReUicUDARTKnA4WTiEcJulJ65E06wKrxf6ijiVtw=

And this is Python code:
import hashlib
import base64
import json
import collections
from urllib.parse import urlencode
from hashlib import blake2b

# Test payload to compare python and js output
payload = {
    'code': 'this_is_fake_code',
    'grant_type': "authorization_code",
    'redirect_uri': 'this_is_redirect_url'
}

digest_str = json.dumps(payload).encode('utf-8')
sha256digest = hashlib.sha256(digest_str).hexdigest()

base64sha256 = base64.b64encode(bytes(sha256digest, 'utf-8')).decode()
# base64sha256 = base64.b64encode(sha256digest).decode()
digest = 'sha-256=' + base64sha256
It produces digest: sha-256=ODhjM2NkMWZjN2I0YTM4M2Q0YzYyZjVmNGIzZjZhMDY1Y2YwZmIzMGJmY2E0NzkwYzk0NjEyMGRkYmUwNjQ0Yw==

Can you help me to create a code that will produce result equal to crypto-js library but in python?
Reply


Messages In This Thread
digest header generating issue - by alexander_pershin - Jan-24-2023, 10:05 AM
RE: digest header generating issue - by wnesbv - Mar-20-2023, 08:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  malformed header from script 'main.py': Bad header: * Serving Flask app "main" anuragsapanbharat 2 4,635 Jun-12-2019, 07:26 AM
Last Post: anuragsapanbharat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020