mirror of
https://github.com/php/php-src.git
synced 2026-03-26 09:12:14 +01:00
- Added new parameter to sha1() and md5() which return the digest as
binary data. (Original patch by Michael Bretterklieber <mbretter@jawa.at>) - Added test cases for sha1() and md5() based on the testvectors in RFC 1321 and RFC 3174. @- Added new parameter to sha1() and md5() which return the digest as @ binary data. (Michael Bretterklieber <mbretter@jawa.at>, Derick)
This commit is contained in:
@@ -40,26 +40,33 @@ PHPAPI void make_digest(char *md5str, unsigned char *digest)
|
||||
*md5str = '\0';
|
||||
}
|
||||
|
||||
/* {{{ proto string md5(string str)
|
||||
/* {{{ proto string md5(string str, [ bool raw_output])
|
||||
Calculate the md5 hash of a string */
|
||||
PHP_NAMED_FUNCTION(php_if_md5)
|
||||
{
|
||||
zval **arg;
|
||||
char *arg;
|
||||
int arg_len;
|
||||
zend_bool raw_output = 0;
|
||||
char md5str[33];
|
||||
PHP_MD5_CTX context;
|
||||
unsigned char digest[16];
|
||||
unsigned char digest[17];
|
||||
|
||||
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(arg);
|
||||
|
||||
|
||||
md5str[0] = '\0';
|
||||
PHP_MD5Init(&context);
|
||||
PHP_MD5Update(&context, Z_STRVAL_PP(arg), Z_STRLEN_PP(arg));
|
||||
PHP_MD5Update(&context, arg, arg_len);
|
||||
PHP_MD5Final(digest, &context);
|
||||
make_digest(md5str, digest);
|
||||
RETVAL_STRING(md5str, 1);
|
||||
if (raw_output) {
|
||||
digest[16] = '\0';
|
||||
RETURN_STRINGL(digest, 16, 1);
|
||||
} else {
|
||||
make_digest(md5str, digest);
|
||||
RETVAL_STRING(md5str, 1);
|
||||
}
|
||||
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
|
||||
@@ -37,26 +37,33 @@ PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest)
|
||||
*sha1str = '\0';
|
||||
}
|
||||
|
||||
/* {{{ proto string sha1(string str)
|
||||
/* {{{ proto string sha1(string str [, bool raw_output])
|
||||
Calculate the sha1 hash of a string */
|
||||
PHP_FUNCTION(sha1)
|
||||
{
|
||||
zval **arg;
|
||||
char *arg;
|
||||
int arg_len;
|
||||
zend_bool raw_output = 0;
|
||||
char sha1str[41];
|
||||
PHP_SHA1_CTX context;
|
||||
unsigned char digest[20];
|
||||
unsigned char digest[21];
|
||||
|
||||
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
|
||||
return;
|
||||
}
|
||||
convert_to_string_ex(arg);
|
||||
|
||||
sha1str[0] = '\0';
|
||||
PHP_SHA1Init(&context);
|
||||
PHP_SHA1Update(&context, Z_STRVAL_PP(arg), Z_STRLEN_PP(arg));
|
||||
PHP_SHA1Update(&context, arg, arg_len);
|
||||
PHP_SHA1Final(digest, &context);
|
||||
make_sha1_digest(sha1str, digest);
|
||||
RETVAL_STRING(sha1str, 1);
|
||||
if (raw_output) {
|
||||
digest[20] = '\0';
|
||||
RETURN_STRINGL(digest, 20, 1);
|
||||
} else {
|
||||
make_sha1_digest(sha1str, digest);
|
||||
RETVAL_STRING(sha1str, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* }}} */
|
||||
|
||||
20
ext/standard/tests/strings/md5.phpt
Normal file
20
ext/standard/tests/strings/md5.phpt
Normal file
@@ -0,0 +1,20 @@
|
||||
--TEST--
|
||||
md5() with ASCII output
|
||||
--FILE--
|
||||
<?php
|
||||
echo md5("")."\n";
|
||||
echo md5("a")."\n";
|
||||
echo md5("abc")."\n";
|
||||
echo md5("message digest")."\n";
|
||||
echo md5("abcdefghijklmnopqrstuvwxyz")."\n";
|
||||
echo md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")."\n";
|
||||
echo md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890")."\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
d41d8cd98f00b204e9800998ecf8427e
|
||||
0cc175b9c0f1b6a831c399e269772661
|
||||
900150983cd24fb0d6963f7d28e17f72
|
||||
f96b697d7cb7938d525a2f31aaf161d0
|
||||
c3fcd3d76192e4007dfb496cca67e13b
|
||||
d174ab98d277d9f5a5611c2c9f419d9f
|
||||
57edf4a22be3c955ac49da2e2107b67a
|
||||
20
ext/standard/tests/strings/md5raw.phpt
Normal file
20
ext/standard/tests/strings/md5raw.phpt
Normal file
@@ -0,0 +1,20 @@
|
||||
--TEST--
|
||||
md5() with RAW output
|
||||
--FILE--
|
||||
<?php
|
||||
echo bin2hex(md5("", TRUE))."\n";
|
||||
echo bin2hex(md5("a", TRUE))."\n";
|
||||
echo bin2hex(md5("abc", TRUE))."\n";
|
||||
echo bin2hex(md5("message digest", TRUE))."\n";
|
||||
echo bin2hex(md5("abcdefghijklmnopqrstuvwxyz", TRUE))."\n";
|
||||
echo bin2hex(md5("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", TRUE))."\n";
|
||||
echo bin2hex(md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890", TRUE))."\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
d41d8cd98f00b204e9800998ecf8427e
|
||||
0cc175b9c0f1b6a831c399e269772661
|
||||
900150983cd24fb0d6963f7d28e17f72
|
||||
f96b697d7cb7938d525a2f31aaf161d0
|
||||
c3fcd3d76192e4007dfb496cca67e13b
|
||||
d174ab98d277d9f5a5611c2c9f419d9f
|
||||
57edf4a22be3c955ac49da2e2107b67a
|
||||
14
ext/standard/tests/strings/sha1.phpt
Normal file
14
ext/standard/tests/strings/sha1.phpt
Normal file
@@ -0,0 +1,14 @@
|
||||
--TEST--
|
||||
sha1() with ASCII output
|
||||
--FILE--
|
||||
<?php
|
||||
echo sha1("abc")."\n";
|
||||
echo sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")."\n";
|
||||
echo sha1("a")."\n";
|
||||
echo sha1("0123456701234567012345670123456701234567012345670123456701234567")."\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
a9993e364706816aba3e25717850c26c9cd0d89d
|
||||
84983e441c3bd26ebaae4aa1f95129e5e54670f1
|
||||
34aa973cd4c4daa4f61eeb2bdbad27316534016f
|
||||
dea356a2cddd90c7a7ecedc5ebb563934f460452
|
||||
14
ext/standard/tests/strings/sha1raw.phpt
Normal file
14
ext/standard/tests/strings/sha1raw.phpt
Normal file
@@ -0,0 +1,14 @@
|
||||
--TEST--
|
||||
sha1() with RAW output
|
||||
--FILE--
|
||||
<?php
|
||||
echo bin2hex(sha1("abc", TRUE))."\n";
|
||||
echo bin2hex(sha1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", TRUE))."\n";
|
||||
echo bin2hex(sha1("a", TRUE))."\n";
|
||||
echo bin2hex(sha1("0123456701234567012345670123456701234567012345670123456701234567", TRUE))."\n";
|
||||
?>
|
||||
--EXPECT--
|
||||
a9993e364706816aba3e25717850c26c9cd0d89d
|
||||
84983e441c3bd26ebaae4aa1f95129e5e54670f1
|
||||
34aa973cd4c4daa4f61eeb2bdbad27316534016f
|
||||
dea356a2cddd90c7a7ecedc5ebb563934f460452
|
||||
Reference in New Issue
Block a user