Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

532
Views
La codificación de Hash en javascript da un resultado diferente en comparación con python

Estoy tratando de convertir un programa de javascript en python, pero obtengo resultados diferentes, después de crear el hash, son los mismos, pero después de codificar los resultados, están muy lejos del de python, ya que es mucho más largo que la versión de javascript.

Resultado de Javascript: 'DqnyGCG5qW5DPceKe1p7fFyFxqdXrqZhQiN2Ukp-2h0'

Resultado de Python: 'NGQ4MWNiYWY3NzgyZmIyZjE3YWEzNWQyNGE1Mjg4M2M2ZmI1MmQxNGE4MDFmNDMxM2FkZWRlOTE1NjVhZGU0YQ'

JavaScript

 var hash = CryptoJS.HmacSHA256(token.join("."), secret); // Same hash on both var base64Hash = CryptoJS.enc.Base64.stringify(hash); document.write(urlConvertBase64(base64Hash)); // Results are different now function urlConvertBase64(input) { var output = input.replace(/=+$/, ''); output = output.replace(/\+/g, '-'); output = output.replace(/\//g, '_'); return output; }

Pitón

 signatureHash = hmac.new(secret.encode(), tokenHB, hashlib.sha256).hexdigest() // Same hash on both signature = base64.urlsafe_b64encode(signatureHash.encode()) signature = signature.rstrip('='.encode()) // Results are different now print(signature)
over 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Codificó dos cosas diferentes entre Node.js y Python. En Node.js, usó directamente el hash sin procesar (valor en bytes del), mientras que en Python usó el valor codificado hexadecimal del mismo hash. Dependiendo de la salida que desee, deberá codificar la salida hexadecimal en Node.js o codificar en base64 el hash sin procesar en Python.

El código de Node.js que replicará ambas versiones de la salida (su implementación de urlConvertBase64 omite en el ejemplo, pero se usa):

 const CryptoJS = require('crypto-js'); var token = 'the token'; var data_to_hash = 'thesecret\x00\x00\x02'; var hashed = CryptoJS.HmacSHA256(data_to_hash, token); var hexdigest = hashed.toString(CryptoJS.enc.Hex); var raw_base64 = urlConvertBase64(CryptoJS.enc.Base64.stringify(hashed)); var hex_base64 = urlConvertBase64(Buffer.from(hexdigest).toString('base64')); console.log(`raw + base64: ${raw_base64}`); console.log(`hex + base64: ${hex_base64}`);

El código de Python que replicará lo anterior:

 import hashlib import hmac from base64 import urlsafe_b64encode token = b'the token' data_to_hash = b'thesecret\x00\x00\x02' hashed = hmac.new(token, data_to_hash, hashlib.sha256) hexdigest = hashed.hexdigest().encode() raw_base64 = urlsafe_b64encode(hashed.digest()).decode().rstrip('=') hex_base64 = urlsafe_b64encode(hexdigest).decode().rstrip('=') print(f'raw + base64: {raw_base64}') print(f'hex + base64: {hex_base64}')

Ambas versiones producen el siguiente resultado:

crudo + base64: R1oLspOvlQ6X_rzaudD8dk8Kqo-nu9pgij-f_-4tnxo
hexadecimal + base64: NDc1YTBiYjI5M2FmOTUwZTk3ZmViY2RhYjlkMGZjNzY0ZjBhYWE4ZmE3YmJkYTYwOGEzZjlmZmZlZTJkOWYxYQ
over 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Virtual assistant