c++ - convert php code to C (sha1 algorithm) -
c++ - convert php code to C (sha1 algorithm) -
php
code:
<?php $pass = "12345678"; $salt = "1234"; echo sha1($salt.$pass.$salt); ?>
my c
code utilize sha1
using openssl crypto library at:http://www.openssl.org/docs/crypto/sha.html.
#include <openssl/sha.h> int main() { const char str[] = "original string"; const char salt[] = "1234"; const char pass[] = "12345678"; strcat(str, salt, pass); unsigned char hash[sha_digest_length]; // == 20 sha1(str, sizeof(str) - 1, hash); // stuff hash homecoming 0; }
my question is, how can modify c
code exact same thing php
code? thanks.
you need allocate plenty space in string concatenated string. also, can't modify const char
, don't utilize modifier on variable you're concatenating into.
char str[17] = ""; // 16 characters plus null terminator const char salt[] = "1234"; const char pass[] = "12345678"; unsigned char hash[sha_digest_length+1]; // +1 null terminator strcpy(str, salt); strcat(str, pass); // strcat() takes 2 arguments, need phone call twice strcat(str, salt); sha1(str, strlen(str), hash);
you should consider using std::string
instead of char arrays in c++.
php c++ cryptography converter sha1
Comments
Post a Comment