Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions | Variables
php_hash.h File Reference
#include "php.h"
#include "php_hash_types.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _php_hash_ops
struct  _php_hash_data

Defines

#define PHP_HASH_EXTNAME   "hash"
#define PHP_HASH_EXTVER   "1.0"
#define PHP_HASH_RESNAME   "Hash Context"
#define PHP_HASH_HMAC   0x0001
#define PHP_HASH_HAVAL_OPS(p, b)   extern const php_hash_ops php_hash_##p##haval##b##_ops;
#define phpext_hash_ptr   &hash_module_entry
#define PHP_HASH_API

Typedefs

typedef void(* php_hash_init_func_t )(void *context)
typedef void(* php_hash_update_func_t )(void *context, const unsigned char *buf, unsigned int count)
typedef void(* php_hash_final_func_t )(unsigned char *digest, void *context)
typedef int(* php_hash_copy_func_t )(const void *ops, void *orig_context, void *dest_context)
typedef struct _php_hash_ops php_hash_ops
typedef struct _php_hash_data php_hash_data

Functions

 PHP_FUNCTION (hash)
 PHP_FUNCTION (hash_file)
 PHP_FUNCTION (hash_hmac)
 PHP_FUNCTION (hash_hmac_file)
 PHP_FUNCTION (hash_init)
 PHP_FUNCTION (hash_update)
 PHP_FUNCTION (hash_update_stream)
 PHP_FUNCTION (hash_update_file)
 PHP_FUNCTION (hash_final)
 PHP_FUNCTION (hash_algos)
PHP_HASH_API const php_hash_opsphp_hash_fetch_ops (const char *algo, int algo_len)
PHP_HASH_API void php_hash_register_algo (const char *algo, const php_hash_ops *ops)
PHP_HASH_API int php_hash_copy (const void *ops, void *orig_context, void *dest_context)
static void php_hash_bin2hex (char *out, const unsigned char *in, int in_len)

Variables

const php_hash_ops php_hash_md2_ops
const php_hash_ops php_hash_md4_ops
const php_hash_ops php_hash_md5_ops
const php_hash_ops php_hash_sha1_ops
const php_hash_ops php_hash_sha224_ops
const php_hash_ops php_hash_sha256_ops
const php_hash_ops php_hash_sha384_ops
const php_hash_ops php_hash_sha512_ops
const php_hash_ops php_hash_ripemd128_ops
const php_hash_ops php_hash_ripemd160_ops
const php_hash_ops php_hash_ripemd256_ops
const php_hash_ops php_hash_ripemd320_ops
const php_hash_ops php_hash_whirlpool_ops
const php_hash_ops php_hash_3tiger128_ops
const php_hash_ops php_hash_3tiger160_ops
const php_hash_ops php_hash_3tiger192_ops
const php_hash_ops php_hash_4tiger128_ops
const php_hash_ops php_hash_4tiger160_ops
const php_hash_ops php_hash_4tiger192_ops
const php_hash_ops php_hash_snefru_ops
const php_hash_ops php_hash_gost_ops
const php_hash_ops php_hash_adler32_ops
const php_hash_ops php_hash_crc32_ops
const php_hash_ops php_hash_crc32b_ops
const php_hash_ops php_hash_salsa10_ops
const php_hash_ops php_hash_salsa20_ops
zend_module_entry hash_module_entry

Class Documentation

struct _php_hash_ops

Definition at line 38 of file php_hash.h.

Class Members
int block_size
int context_size
int digest_size
php_hash_copy_func_t hash_copy
php_hash_final_func_t hash_final
php_hash_init_func_t hash_init
php_hash_update_func_t hash_update
struct _php_hash_data

Definition at line 49 of file php_hash.h.

Collaboration diagram for _php_hash_data:
Class Members
void * context
unsigned char * key
const php_hash_ops * ops
long options

Define Documentation

#define PHP_HASH_API

Definition at line 112 of file php_hash.h.

#define PHP_HASH_EXTNAME   "hash"

Definition at line 27 of file php_hash.h.

#define PHP_HASH_EXTVER   "1.0"

Definition at line 28 of file php_hash.h.

#define PHP_HASH_HAVAL_OPS (   p,
 
)    extern const php_hash_ops php_hash_##p##haval##b##_ops;

Definition at line 84 of file php_hash.h.

#define PHP_HASH_HMAC   0x0001

Definition at line 31 of file php_hash.h.

#define PHP_HASH_RESNAME   "Hash Context"

Definition at line 29 of file php_hash.h.

Definition at line 105 of file php_hash.h.


Typedef Documentation

typedef int(* php_hash_copy_func_t)(const void *ops, void *orig_context, void *dest_context)

Definition at line 36 of file php_hash.h.

typedef struct _php_hash_data php_hash_data
typedef void(* php_hash_final_func_t)(unsigned char *digest, void *context)

Definition at line 35 of file php_hash.h.

typedef void(* php_hash_init_func_t)(void *context)

Definition at line 33 of file php_hash.h.

typedef struct _php_hash_ops php_hash_ops
typedef void(* php_hash_update_func_t)(void *context, const unsigned char *buf, unsigned int count)

Definition at line 34 of file php_hash.h.


Function Documentation

PHP_FUNCTION ( hash  )

Definition at line 182 of file hash.c.

Here is the call graph for this function:

PHP_FUNCTION ( hash_file  )

Definition at line 191 of file hash.c.

Here is the call graph for this function:

PHP_FUNCTION ( hash_hmac  )

Definition at line 294 of file hash.c.

Here is the call graph for this function:

PHP_FUNCTION ( hash_hmac_file  )

Definition at line 303 of file hash.c.

Here is the call graph for this function:

PHP_FUNCTION ( hash_init  )

Definition at line 312 of file hash.c.

{
       char *algo, *key = NULL;
       int algo_len, key_len = 0, argc = ZEND_NUM_ARGS();
       long options = 0;
       void *context;
       const php_hash_ops *ops;
       php_hash_data *hash;

       if (zend_parse_parameters(argc TSRMLS_CC, "s|ls", &algo, &algo_len, &options, &key, &key_len) == FAILURE) {
              return;
       }

       ops = php_hash_fetch_ops(algo, algo_len);
       if (!ops) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
              RETURN_FALSE;
       }

       if (options & PHP_HASH_HMAC &&
              key_len <= 0) {
              /* Note: a zero length key is no key at all */
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "HMAC requested without a key");
              RETURN_FALSE;
       }

       context = emalloc(ops->context_size);
       ops->hash_init(context);

       hash = emalloc(sizeof(php_hash_data));
       hash->ops = ops;
       hash->context = context;
       hash->options = options;
       hash->key = NULL;

       if (options & PHP_HASH_HMAC) {
              char *K = emalloc(ops->block_size);
              int i;

              memset(K, 0, ops->block_size);

              if (key_len > ops->block_size) {
                     /* Reduce the key first */
                     ops->hash_update(context, (unsigned char *) key, key_len);
                     ops->hash_final((unsigned char *) K, context);
                     /* Make the context ready to start over */
                     ops->hash_init(context);
              } else {
                     memcpy(K, key, key_len);
              }
                     
              /* XOR ipad */
              for(i=0; i < ops->block_size; i++) {
                     K[i] ^= 0x36;
              }
              ops->hash_update(context, (unsigned char *) K, ops->block_size);
              hash->key = (unsigned char *) K;
       }

       ZEND_REGISTER_RESOURCE(return_value, hash, php_hash_le_hash);
}

Here is the call graph for this function:

PHP_FUNCTION ( hash_update  )

Definition at line 377 of file hash.c.

{
       zval *zhash;
       php_hash_data *hash;
       char *data;
       int data_len;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zhash, &data, &data_len) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);

       hash->ops->hash_update(hash->context, (unsigned char *) data, data_len);

       RETURN_TRUE;
}

Here is the call graph for this function:

PHP_FUNCTION ( hash_update_stream  )

Definition at line 398 of file hash.c.

{
       zval *zhash, *zstream;
       php_hash_data *hash;
       php_stream *stream = NULL;
       long length = -1, didread = 0;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &zhash, &zstream, &length) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
       php_stream_from_zval(stream, &zstream);

       while (length) {
              char buf[1024];
              long n, toread = 1024;

              if (length > 0 && toread > length) {
                     toread = length;
              }

              if ((n = php_stream_read(stream, buf, toread)) <= 0) {
                     /* Nada mas */
                     RETURN_LONG(didread);
              }
              hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
              length -= n;
              didread += n;
       } 

       RETURN_LONG(didread);
}

Here is the call graph for this function:

PHP_FUNCTION ( hash_update_file  )

Definition at line 435 of file hash.c.

{
       zval *zhash, *zcontext = NULL;
       php_hash_data *hash;
       php_stream_context *context;
       php_stream *stream;
       char *filename, buf[1024];
       int filename_len, n;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|r", &zhash, &filename, &filename_len, &zcontext) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
       context = php_stream_context_from_zval(zcontext, 0);

       stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL, context);
       if (!stream) {
              /* Stream will report errors opening file */
              RETURN_FALSE;
       }

       while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
              hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
       }
       php_stream_close(stream);

       RETURN_TRUE;
}

Here is the call graph for this function:

PHP_FUNCTION ( hash_final  )

Definition at line 468 of file hash.c.

{
       zval *zhash;
       php_hash_data *hash;
       zend_bool raw_output = 0;
       zend_rsrc_list_entry *le;
       char *digest;
       int digest_len;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zhash, &raw_output) == FAILURE) {
              return;
       }

       ZEND_FETCH_RESOURCE(hash, php_hash_data*, &zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);

       digest_len = hash->ops->digest_size;
       digest = emalloc(digest_len + 1);
       hash->ops->hash_final((unsigned char *) digest, hash->context);
       if (hash->options & PHP_HASH_HMAC) {
              int i;

              /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
              for(i=0; i < hash->ops->block_size; i++) {
                     hash->key[i] ^= 0x6A;
              }

              /* Feed this result into the outter hash */
              hash->ops->hash_init(hash->context);
              hash->ops->hash_update(hash->context, (unsigned char *) hash->key, hash->ops->block_size);
              hash->ops->hash_update(hash->context, (unsigned char *) digest, hash->ops->digest_size);
              hash->ops->hash_final((unsigned char *) digest, hash->context);

              /* Zero the key */
              memset(hash->key, 0, hash->ops->block_size);
              efree(hash->key);
              hash->key = NULL;
       }
       digest[digest_len] = 0;
       efree(hash->context);
       hash->context = NULL;

       /* zend_list_REAL_delete() */
       if (zend_hash_index_find(&EG(regular_list), Z_RESVAL_P(zhash), (void *) &le)==SUCCESS) {
              /* This is a hack to avoid letting the resource hide elsewhere (like in separated vars)
                     FETCH_RESOURCE is intelligent enough to handle dealing with any issues this causes */
              le->refcount = 1;
       } /* FAILURE is not an option */
       zend_list_delete(Z_RESVAL_P(zhash));

       if (raw_output) {
              RETURN_STRINGL(digest, digest_len, 0);
       } else {
              char *hex_digest = safe_emalloc(digest_len,2,1);

              php_hash_bin2hex(hex_digest, (unsigned char *) digest, digest_len);
              hex_digest[2 * digest_len] = 0;
              efree(digest);
              RETURN_STRINGL(hex_digest, 2 * digest_len, 0);          
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( hash_algos  )

Definition at line 569 of file hash.c.

{
       HashPosition pos;
       char *str;
       uint str_len;
       long type;
       ulong idx;

       array_init(return_value);
       for(zend_hash_internal_pointer_reset_ex(&php_hash_hashtable, &pos);
              (type = zend_hash_get_current_key_ex(&php_hash_hashtable, &str, &str_len, &idx, 0, &pos)) != HASH_KEY_NON_EXISTANT;
              zend_hash_move_forward_ex(&php_hash_hashtable, &pos)) {
              add_next_index_stringl(return_value, str, str_len-1, 1);
       }
}

Here is the call graph for this function:

static void php_hash_bin2hex ( char *  out,
const unsigned char *  in,
int  in_len 
) [inline, static]

Definition at line 134 of file php_hash.h.

{
       static const char hexits[17] = "0123456789abcdef";
       int i;

       for(i = 0; i < in_len; i++) {
              out[i * 2]       = hexits[in[i] >> 4];
              out[(i * 2) + 1] = hexits[in[i] &  0x0F];
       }
}

Here is the caller graph for this function:

PHP_HASH_API int php_hash_copy ( const void *  ops,
void *  orig_context,
void *  dest_context 
)

Definition at line 109 of file hash.c.

{
       php_hash_ops *hash_ops = (php_hash_ops *)ops;

       memcpy(dest_context, orig_context, hash_ops->context_size);
       return SUCCESS;
}
PHP_HASH_API const php_hash_ops* php_hash_fetch_ops ( const char *  algo,
int  algo_len 
)

Definition at line 83 of file hash.c.

{
       php_hash_ops *ops;
       char *lower = estrndup(algo, algo_len);

       zend_str_tolower(lower, algo_len);
       if (SUCCESS != zend_hash_find(&php_hash_hashtable, lower, algo_len + 1, (void*)&ops)) {
              ops = NULL;
       }
       efree(lower);

       return ops;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHP_HASH_API void php_hash_register_algo ( const char *  algo,
const php_hash_ops ops 
)

Definition at line 98 of file hash.c.

{
       int algo_len = strlen(algo);
       char *lower = estrndup(algo, algo_len);
       
       zend_str_tolower(lower, algo_len);
       zend_hash_add(&php_hash_hashtable, lower, algo_len + 1, (void*)ops, sizeof(php_hash_ops), NULL);
       efree(lower);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

zend_module_entry hash_module_entry

Definition at line 1069 of file hash.c.

Definition at line 58 of file hash_adler32.c.

Definition at line 75 of file hash_crc32.c.

Definition at line 85 of file hash_crc32.c.

Definition at line 305 of file hash_gost.c.

Definition at line 44 of file hash_md.c.

Definition at line 34 of file hash_md.c.

Definition at line 24 of file hash_md.c.

Definition at line 28 of file hash_ripemd.c.

Definition at line 38 of file hash_ripemd.c.

Definition at line 48 of file hash_ripemd.c.

Definition at line 58 of file hash_ripemd.c.

Definition at line 197 of file hash_salsa.c.

Definition at line 207 of file hash_salsa.c.

Definition at line 69 of file hash_sha.c.

Definition at line 421 of file hash_sha.c.

Definition at line 411 of file hash_sha.c.

Definition at line 913 of file hash_sha.c.

Definition at line 1027 of file hash_sha.c.

Definition at line 196 of file hash_snefru.c.

Definition at line 436 of file hash_whirlpool.c.