예제 #1
0
파일: ext_hash.cpp 프로젝트: shixiao/hhvm
Variant HHVM_FUNCTION(hash_final, const Resource& context,
                                 bool raw_output /* = false */) {
  auto hash = dyn_cast<HashContext>(context);

  if (hash->context == nullptr) {
    raise_warning(
      "hash_final(): supplied resource is not a valid Hash Context resource"
    );
    return false;
  }

  String raw = String(hash->ops->digest_size, ReserveString);
  char *digest = raw.mutableData();
  hash->ops->hash_final((unsigned char *)digest, hash->context);
  if (hash->options & k_HASH_HMAC) {
    finalize_hmac_key(hash->key, hash->ops, hash->context, digest);
    hash->key = NULL;
  }
  free(hash->context);
  hash->context = NULL;

  raw.setSize(hash->ops->digest_size);
  if (raw_output) {
    return raw;
  }
  return HHVM_FN(bin2hex)(raw);
}
예제 #2
0
String f_hash_final(CObjRef context, bool raw_output /* = false */) {
  HashContext *hash = context.getTyped<HashContext>();

  String raw = String(hash->ops->digest_size, ReserveString);
  char *digest = raw.mutableSlice().ptr;
  hash->ops->hash_final((unsigned char *)digest, hash->context);
  if (hash->options & k_HASH_HMAC) {
    finalize_hmac_key(hash->key, hash->ops, hash->context, digest);
    hash->key = NULL;
  }
  free(hash->context);
  hash->context = NULL;

  raw.setSize(hash->ops->digest_size);
  if (raw_output) {
    return raw;
  }
  return StringUtil::HexEncode(raw);
}
예제 #3
0
static Variant php_hash_do_hash_hmac(CStrRef algo, CStrRef data,
                                     bool isfilename, CStrRef key,
                                     bool raw_output /* = false */) {
  HashEnginePtr ops = php_hash_fetch_ops(algo);
  if (!ops) {
    raise_warning("Unknown hashing algorithm: %s", algo.data());
    return false;
  }
  Variant f;
  if (isfilename) {
    f = f_fopen(data, "rb");
    if (same(f, false)) {
      return false;
    }
  }

  void *context = malloc(ops->context_size);
  ops->hash_init(context);

  char *K = prepare_hmac_key(ops, context, key);

  if (isfilename) {
    for (Variant chunk = f_fread(f, 1024); !is_empty_string(chunk);
         chunk = f_fread(f, 1024)) {
      String schunk = chunk.toString();
      ops->hash_update(context, (unsigned char *)schunk.data(), schunk.size());
    }
  } else {
    ops->hash_update(context, (unsigned char *)data.data(), data.size());
  }

  String raw = String(ops->digest_size, ReserveString);
  char *digest = raw.mutableSlice().ptr;
  ops->hash_final((unsigned char *)digest, context);
  finalize_hmac_key(K, ops, context, digest);
  free(context);

  raw.setSize(ops->digest_size);
  if (raw_output) {
    return raw;
  }
  return StringUtil::HexEncode(raw);
}
예제 #4
0
파일: ext_hash.cpp 프로젝트: KOgames/hhvm
Variant HHVM_FUNCTION(hash_final, const Resource& context,
                                 bool raw_output /* = false */) {
  auto hash = get_valid_hash_context_resource(context, __FUNCTION__);
  if (!hash) {
    return false;
  }

  String raw = String(hash->ops->digest_size, ReserveString);
  char *digest = raw.mutableData();
  hash->ops->hash_final((unsigned char *)digest, hash->context);
  if (hash->options & k_HASH_HMAC) {
    finalize_hmac_key(hash->key, hash->ops, hash->context, digest);
    hash->key = NULL;
  }
  free(hash->context);
  hash->context = NULL;

  raw.setSize(hash->ops->digest_size);
  if (raw_output) {
    return raw;
  }
  return HHVM_FN(bin2hex)(raw);
}