• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

268
Views
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 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error