Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

350
Visualizações
Ruby OpenSSL to node.js Crypt conversion

I have been struggling with this for a couple of days and was wondering if anyone would have the experience to know these two encryption libraries well enough to help.

I am currently creating a SSO payload according to instructions given to me by a vendor. The steps to have this created are highlighted as follows:

Create an AES 256 CBC cypher of the payload
i. The key will be a SHA256 digest of the site token.

2. Base64 encode the initialization vector (IV) and encrypted payload from above

3. CGI-escape the output from step 2.

4. Your final payload would look something like
ikUbqiutwMhi%2Bjg6WwUHyeZB76g6LdLGcrKrEV4YpvQ%3D%0A.

SHA256 will always generate a 32-byte hash, but it can’t be displayed nicely in Base64. When it’s displayed as Hex, it is 32 pairs of Hex values (a total of 64 characters on the screen) but representing only 32 bytes.

I was able to get it to work on Ruby with Open SSL, the code is:

require 'digest'
require 'openssl'
require "base64"
require 'cgi'
require 'json'


cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.encrypt
cipher.key = Digest::SHA256.digest(siteToken)
iv = cipher.random_iv
data= unencryptedPayload
encrypted = cipher.update(JSON.generate(data)) + cipher.final
encoded = CGI::escape(Base64.encode64(iv + encrypted))
puts encoded

However, I have not yet had luck with Node.js's Crypto library. This is what I have so far:

const crypto = require('crypto');


// Defining algorithm
const algorithm = 'aes-256-cbc';
 
// Defining key
//'key' variable is defined and equal to siteToken in the OpenSSL version
//const key = siteToken;
 
// Defining iv
const iv = crypto.randomBytes(16);
 
// An encrypt function
function encrypt(text) {
 
 // Creating Cipheriv with its parameter
 let cipher = crypto.createCipheriv(
      'aes-256-cbc', Buffer.from(key), iv);
 
 // Updating text
 let encrypted = cipher.update(text);
 
 // Using concatenation
 encrypted = Buffer.concat([encrypted, cipher.final()]);
 
 // Returning iv and encrypted data
 return { iv: iv.toString('hex'),
    encryptedData: encrypted.toString('hex') };
}
 
// Displays output
var output = encrypt(unencryptedPayload);

I think my code has so far covered almost all of these except for the SHA256 digest of the site token. Does anyone know how I might achieve this in Node.js terms?

Thanks!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda