您的问题的解决方案:
Python2:
from collections import OrderedDict
import json
import hmac
import hashlib
import base64
key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")
od = OrderedDict()
od['personNumber'] = "195012161930"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"
contentToHash = json.dumps(od,indent=2,separators=(',', ': '))
contentToHash = contentToHash.replace("\n","\r\n")
print(repr(contentToHash))
hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)
baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")
Python3:
from collections import OrderedDict
import json
import hmac
import hashlib
import base64
key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")
od = OrderedDict()
od['personNumber'] = "195012161930"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"
contentToHash = json.dumps(od,indent="\r ",separators=(',', ': '))
contentToHash=contentToHash.replace("\n\r","\r\n")
contentToHash = contentToHash.replace("\n}","\r\n}")
print(repr(contentToHash))
hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)
baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")
邮递员:
var hmacKey = 'c811f8ae-dd9f-4b15-9a09-97a09bdbb485';
var contentToHash = pm.request.body.raw;
console.log(JSON.stringify(contentToHash))
var cryptoJs = require('crypto-js');
var hash = cryptoJs.HmacSHA256(contentToHash, hmacKey);
var hashInBase64 = cryptoJs.enc.Base64.stringify(hash);
console.log(hashInBase64 + " This is the result");
正确做法:
您应该删除空格以使其适用于所有应用程序:
pm.request.body 不仅返回正文内容,还返回具有内容类型的完整正文对象
所以你应该使用
pm.request.body.raw
但这仍然会有空间,你可以使用删除它
JSON.stringify(JSON.parse(pm.request.body.raw))
现在,python 代码中存在相同的空间问题,您可以使用它来删除它
contentToHash = json.dumps(od,separators=(',', ':'))
Python 代码:
from collections import OrderedDict
import json
import hmac
import hashlib
import base64
key = bytearray("c811f8ae-dd9f-4b15-9a09-97a09bdbb485","utf-8")
od = OrderedDict()
od['personNumber'] = "195012161111"
od['requestDescription'] = "Detta ar ett test"
od['verksamhetId'] = "1ac12a80-819a-42ca-bd51-97bd3e19c443"
od['stadsDelID'] = "f55a5bea-2398-11e9-8140-0e9ccdb68c09"
contentToHash = json.dumps(od,separators=(',', ':'))
hash = hmac.new( key, (contentToHash).encode('utf-8'), hashlib.sha256)
baseHash = base64.b64encode(hash.digest())
print(str(baseHash) + " This is the result in Python")
邮递员预请求:
var hmacKey = 'c811f8ae-dd9f-4b15-9a09-97a09bdbb485';
var contentToHash = JSON.stringify(JSON.parse(pm.request.body.raw),null,null)
var cryptoJs = require('crypto-js');
var hash = cryptoJs.HmacSHA256(contentToHash, hmacKey);
console.log(hash)
var hashInBase64 = cryptoJs.enc.Base64.stringify(hash);
console.log(hashInBase64 + " This is the result");