/*
`string.hash(file, [algorithm], callback)`
------------------------------------------
Output the hash of a supplied string in hexadecimal
form. The default algorithm to compute the hash is md5.
184809
function(data, algorithm) {
    if (arguments.length === 1) {
      algorithm = 'md5';
    }
    return crypto.createHash(algorithm).update(data).digest('hex');
  }
259005
function stringToHash(hashAsString) {
        var entries = {};
        if (hashAsString) {
            var text = decodeURI(hashAsString);
            var items = text.split('&');
            angular.forEach(items, function (item) {
                var kv = item.split('=');
                var key = kv[0];
                var value = kv[1] || key;
                entries[key] = value;
            });
        }
        return entries;
    }
1094924
public static String hashToString(byte[] hash) {
        StringBuilder buf = new StringBuilder();
        for (byte b : hash) {
            buf.append(HEX_CHARS[(b >> 4) & 0xF]);
            buf.append(HEX_CHARS[b & 0xF]);
        }
        return buf.toString();
    }
1065611
function hashDocument(document) {
  var words;

  if (Buffer.isBuffer(document)) {
    words = Base64.parse(document.toString('base64'));
  } else if (typeof document === 'string') {
    words = Base64.parse(document);
  } else {
    throw new TypeError('Expected document to be Buffer or String');
  }

  var hash = sha256(words);

  return hash.toString(Hex);
}
684039
static String hashMD5(String s) throws ApiDataException {
        MessageDigest algorithm = null;
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            throw new ApiDataException("Cannot find MD5 digest algorithm");
        }
        byte[] defaultBytes = new byte[s.length()];
        for (int i = 0; i < s.length(); i++) {
            defaultBytes[i] = (byte) (0xFF & s.charAt(i));
        }
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < messageDigest.length; i++) {
            String hex = Integer.toHexString(0xFF & messageDigest[i]);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
525209
def md5_hash_file(fh):
    """"""
    md5 = hashlib.md5()
    while True:
        data = fh.read(8192)
        if not data:
            break
        md5.update(data)
    return md5.hexdigest()
246888
public static function hash($string)
    {
        static $valid_hash = null;
        if ($valid_hash === null) {
            $valid_hash = function_exists('hash') && (in_array('sha256', hash_algos()));
        }

        /**
         * Fallback hash('sha256', [(string) string]);
         */
        if ($valid_hash) {
            return hash('sha256', $string);
        }

        if (is_array($string) || is_object($string)) {
            $type   = gettype($string);
            $caller =  next(debug_backtrace());
            $error['line']  = $caller['line'];
            $error['file']  = strip_tags($caller['file']);
            trigger_error(
                "sha1() expects parameter 1 to be string, "
                . $type
                . " given in <b>{$error['file']}</b> on line <b>{$error['line']}</b><br />\n",
                E_USER_ERROR
            );

            return null;
        }

        // convert into string
        $string = "{$string}";
        /**
         * Instance Application
         * @var object
         */
        $instance = self::getInstance();
        $key = md5($string);
        if (isset($instance->x_sha256_record[$key])) {
            return $instance->x_sha256_record[$key];
        }

        /**
         * SHA-256 Constants
         * sequence of 64 constant 32-bit words representing the first thirty-two bits
         * of the fractional parts of the cube roots of the first 64 prime numbers.
         * @var  array
         */
        $keyState64 = array(
                   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
                   0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
                   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
                   0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
                   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
                   0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
                   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
                   0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
                   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
                   0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
                   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
                   0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
                   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
                   0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
                   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
                   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
        /**
         * [$state8 description]
         * @var array
         */
        $state8 = array(
            0x6a09e667,
            0xbb67ae85,
            0x3c6ef372,
            0xa54ff53a,
            0x510e527f,
            0x9b05688c,
            0x1f83d9ab,
            0x5be0cd19
        );
        /**
         *  Parsing the Padded Message (Break into N 512-bit blocks)
         * @var array
         */
        $string_split = str_split(
            $instance->charPad($string), // Pre-processing: Padding the string
            64
        );

        // loop through message blocks and compute hash. ( For i=1 to N : )
        for ($i = 0; $i < count($string_split); $i++) {
            // Break input block into 16 32-bit words (message schedule prep)
            $MI = parent::byte2intSplit($string_split[$i]);
            // Initialize working variables
            $a = $state8[0];
            $b = $state8[1];
            $c = $state8[2];
            $d = $state8[3];
            $e = $state8[4];
            $f = $state8[5];
            $g = $state8[6];
            $h = $state8[7];
            $W = array();
            // Compute the hash and update
            for ($t = 0; $t < 16; $t++) {
                // Prepare the first 16 message schedule values as we loop
                $W[$t] = $MI[$t];
                $t1 = ($e & $f) ^ ((~$e) & $g); //$instance->Ch($e, $f, $g);
                $t1 = $instance->add(
                    $instance->add(
                        $h,
                        ($instance->rotr($e, 6)^$instance->rotr($e, 11)^$instance->rotr($e, 25)) //$instance->Sigma1($e)
                    ),
                    $t1
                );
                // Compute hash
                $t1 = $instance->add(
                    $instance->add(
                        $t1,
                        $keyState64[$t]
                    ),
                    $W[$t]
                );
                $t2 = $instance->add(
                    ($instance->rotr($a, 2)^$instance->rotr($a, 13)^$instance->rotr($a, 22)), //$instance->Sigma0($a),
                    (($a & $b) ^ ($a & $c) ^ ($b & $c)) //$instance->Maj($a, $b, $c)
                );

                // Update working variables
                $h = $g;
                $g = $f;
                $f = $e;
                $e = $instance->add($d, $t1);
                $d = $c;
                $c = $b;
                $b = $a;
                $a = $instance->add($t1, $t2);
            }

            for (; $t < 64; $t++) {
                // Continue building the message schedule as we loop
                $s0 = $W[($t+1)&0x0F];
                $s0 =($instance->rotr($s0, 7)^$instance->rotr($s0, 18)^parent::zeroFill($s0, 3));
                $s1 = $W[($t+14)&0x0F];
                $s1 = $instance->rotr($s1, 17)^$instance->rotr($s1, 19)^parent::zeroFill($s1, 10);
                $W[$t&0xF] = $instance->add($W[$t&0xF], $s0);
                $W[$t&0xF] = $instance->add($W[$t&0xF], $s1);
                $W[$t&0xF] = $instance->add($W[$t&0xF], $W[($t+9)&0x0F]);

                // Compute hash
                $t1 = $instance->add($h, ($instance->rotr($e, 6)^$instance->rotr($e, 11)^$instance->rotr($e, 25)));
                $t1 = $instance->add($t1, (($e & $f) ^ ((~$e) & $g)));
                $t1 = $instance->add($t1, $keyState64[$t]);
                $t1 = $instance->add($t1, $W[$t&0xF]);
                $t2 = $instance->add(
                    ($instance->rotr($a, 2)^$instance->rotr($a, 13)^$instance->rotr($a, 22)),
                    ($a & $b) ^ ($a & $c) ^ ($b & $c)
                );

                // Update working variables
                $h = $g;
                $g = $f;
                $f = $e;
                $e = $instance->add($d, $t1);
                $d = $c;
                $c = $b;
                $b = $a;
                $a = $instance->add($t1, $t2);
            }

            $state8[0] = $instance->add($state8[0], $a);
            $state8[1] = $instance->add($state8[1], $b);
            $state8[2] = $instance->add($state8[2], $c);
            $state8[3] = $instance->add($state8[3], $d);
            $state8[4] = $instance->add($state8[4], $e);
            $state8[5] = $instance->add($state8[5], $f);
            $state8[6] = $instance->add($state8[6], $g);
            $state8[7] = $instance->add($state8[7], $h);
        }
        // Convert the 32-bit words into human readable hexadecimal format.
        $instance->x_sha256_record[$key] = sprintf(
            "%08x%08x%08x%08x%08x%08x%08x%08x",
            $state8[0],
            $state8[1],
            $state8[2],
            $state8[3],
            $state8[4],
            $state8[5],
            $state8[6],
            $state8[7]
        );
        $ret_val = $instance->x_sha256_record[$key];
        unset(
            $string,
            $W,
            $instance,
            $binStr,
            $keyState64,
            $string_split,
            $state8,
            $MI,
            $s0,
            $s1,
            $t1,
            $t2
        );
        return $ret_val;
    }
1183242
public static String getHashText(String plainText, String algorithm) throws NoSuchAlgorithmException {
	MessageDigest mdAlgorithm = MessageDigest.getInstance(algorithm);

	mdAlgorithm.update(plainText.getBytes());

	byte[] digest = mdAlgorithm.digest();
	StringBuffer hexString = new StringBuffer();

	for (int i = 0; i < digest.length; i++) {
	    plainText = Integer.toHexString(0xFF & digest[i]);

	    if (plainText.length() < 2) {
		plainText = "0" + plainText;
	    }

	    hexString.append(plainText);
	}

	return hexString.toString();
    }
381518
def hash(file):
    

    exists(file)
    log(_("hashing {}...").format(file))

    # https://stackoverflow.com/a/22058673
    with open(file, "rb") as f:
        sha256 = hashlib.sha256()
        for block in iter(lambda: f.read(65536), b""):
            sha256.update(block)
        return sha256.hexdigest()
357528
function md5(files) {
		var hash = crypto.createHash('md5');
		_.each(files, function(file) {
			if (!isExternal(file))
				hash.update(grunt.file.read(file), 'utf-8');
		});
    	return hash.digest('hex');
	}
710107
function hashsum(hash, str) {
        // you have to convert from string to array buffer
      var ab
        // you have to represent the algorithm as an object 
        , algo = { name: algos[hash] }
        ;

      if ('string' === typeof str) {
        ab = str2ab(str);
      } else {
        ab = str;
      }

      // All crypto digest methods return a promise
      return crypto.subtle.digest(algo, ab).then(function (digest) {
        // you have to convert the ArrayBuffer to a DataView and then to a hex String
        return ab2hex(digest);
      }).catch(function (e) {
        // if you specify an unsupported digest algorithm or non-ArrayBuffer, you'll get an error
        console.error('sha1sum ERROR');
        console.error(e);
        throw e;
      });
    }
488932
function hash(str, algo, encoding) {
  return crypto
    .createHash(algo || 'sha1')
    .update(str)
    .digest(encoding || 'hex');
}
392363
function hash(input, algo, type){
    // string or buffer input ? => keep it
    if (typeof input !== 'string' && !(input instanceof Buffer)){
        input = JSON.stringify(input);
    }

    // create hash algo
    var sum = _crypto.createHash(algo);

    // set content
    sum.update(input);

    // binary output ?
    if (type && type.toLowerCase().trim() == 'binary'){
        // calculate hashsum
        return sum.digest();

    // base 64 urlsafe ?
    }else if (type==='base64-urlsafe'){
        return base64urlsafe(sum.digest('base64'));

    // string output
    }else{
        // calculate hashsum
        return sum.digest(type);
    }
}
254382
function objectHash(obj) {
	var hash = crypto.createHash('md5');
	hash.update(makeHashKey(obj));
	return hash.digest('hex');
}
54451
function(input) {
  input = input.toString();
  var hash = 0, i, chr, len = input.length;
  if (len === 0) {
    return hash;
  }
  for (i = 0; i < len; i++) {
    chr = input.charCodeAt(i);
    hash = (hash << 5) - hash + chr;
    hash |= 0;
  }
  return hash;
}
339159
function hash(s) {
    var h = hash.BASE

    for (var i = 0, l = s.length; i < l; i++) {
        h ^= s.charCodeAt(i)
        h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24)
    }

    return h >>> 0
}
110265
function processFileBySha1Hash(stream, firstPass, basePath, file, cache) {
  // null cannot be hashed
  if (file.contents === null) {
    // if element is really a file, something weird happened, but it's safer
    // to assume it was changed (because we cannot said that it wasn't)
    // if it's not a file, we don't care, do we? does anybody transform directories?
    if (file.stat.isFile()) {
      stream.push(file);
    }
  } else {
    var newHash = crypto.createHash('sha1').update(file.contents).digest('hex');
    var filePath = basePath ? path.relative(basePath, file.path) : file.path;
    var currentHash = cache[filePath];

    cache[filePath] = newHash;

    if ((!currentHash && firstPass) || (currentHash && currentHash !== newHash)) {
      stream.push(file);
    }
  }
}