I have no time to do it so I need someone to convert two functions from PHP code to Node JS.
- Make this functions easy to use by creating a module so I can just do something like > var encdec = require('encdec'); < (this is not for redistribute, it is for a private project, so no need manuals nor any extras) and then I can just do > [login to view URL]('myEncryptedString'); <
- This one returns a false on fail (which never happen) but try to do it error free as much as possible.
/**
* 256-bit decryption with mcrypt
* MCRYPT_RIJNDAEL_128 is AES-compliant, and MCRYPT_MODE_CBC
* Note: ECB mode is inadequate for many purposes because it does not use an IV
*
* @param string $encrypted receives the encrypted string to decrypt
* return bool false on fail, or decrypted string if succeeded
*/
function myDecrypt ($encrypted)
{
// salt
$salt = '!kqM*Ff7pXe1kBn%9';
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt ."LPC9aVs3w8k45vx7oHOxZgoNus3banTGPp5wmhJ6hSnJko9rjB/G5Q1mdhdxjm01q6kK2h44MP5L6DML4nY/uq", true);
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
$iv = base64_decode(substr($encrypted, 0, 22) . '==');
// Remove $iv from $encrypted.
$encrypted = substr($encrypted, 22);
// Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
@$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
// Retrieve $hash which is the last 32 characters of $decrypted.
$hash = substr($decrypted, - 32);
// Remove the last 32 characters from $decrypted.
$decrypted = substr($decrypted, 0, - 32);
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
if (md5($decrypted) != $hash)
return false;
// Yay!
return $decrypted;
}
/**
* 256-bit encryption with mcrypt
* MCRYPT_RIJNDAEL_128 is AES-compliant, and MCRYPT_MODE_CBC
* Note: ECB mode is inadequate for many purposes because it does not use an IV
*
* @param string $decrypted receives the unencrypted string to encrypt
* return bool false on fail, or encrypted string if succeeded
*/
function myEncrypt($decrypted) {
// salt
$salt = '!kqM*Ff7pXe1kBn%9';
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt ."LPC9aVs3w8k45vx7oHOxZgoNus3banTGPp5wmhJ6hSnJko9rjB/G5Q1mdhdxjm01q6kK2h44MP5L6DML4nY/uq", true);
// Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
srand();
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22)
return false;
// Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it's just to verify successful decryption.
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
// We're done!
return $iv_base64 . $encrypted;
}
Hi, after reading your requirements i decided to place a bid on this project.
I lead a team of node.js development. My total experience in node.js is around three years.
overall IT experience is 7 years.
We have experienced team of experts of Node.js, html designers, mongodb and web applications.
We can discuss more on skype username 'morwal89'.
Hope to hear back.
Thank you.
Hello, The task is trivial. I will create a node module (wrapper over crypto module) which exports two functions: encrypt and decrypt. I have a 4 years experience in designing complex software using python and nodejs.
Thank you and good luck!
I am a highly experienced developer and very familiar with javascript and node.js
Your task is straight-forward and I will complete it very quickly with unit tests included.