Creates a digest hash from the inputted string,
and the specified or the preferred hash algorithm.

@param string $data Data to be hashed.
@param string $hash hash algoritm to use

@throws KlarnaException
@return string  Base64 encoded hash.
183515
public static function digest($data, $hash = null)
    {
        if ($hash===null) {
            $preferred = array(
                'sha512',
                'sha384',
                'sha256',
                'sha224',
                'md5'
            );

            $hashes = array_intersect($preferred, hash_algos());

            if (count($hashes) == 0) {
                throw new KlarnaException(
                    "No available hash algorithm supported!"
                );
            }
            $hash = array_shift($hashes);
        }
        self::printDebug('digest() using hash', $hash);

        return base64_encode(pack("H*", hash($hash, $data)));
    }
386112
protected function loadPyStringHash(array $hash, $line = 0)
    {
        $line = isset($hash['line']) ? $hash['line'] : $line;

        $strings = array();
        foreach (explode("\n", $hash['text']) as $string) {
            $strings[] = $string;
        }

        return new PyStringNode($strings, $line);
    }
38169
function generateHash(data) {
        var deferred = Q.defer();

        bcrypt.hash(data, 10, function (err, res) {
            err ? deferred.reject(err) : deferred.resolve(res);
        });

        return deferred.promise;
    }
875018
function setHash($hash)
    {
        // \phpseclib\Crypt\Hash supports algorithms that PKCS#1 doesn't support.  md5-96 and sha1-96, for example.
        switch ($hash) {
            case 'md2':
            case 'md5':
            case 'sha1':
            case 'sha256':
            case 'sha384':
            case 'sha512':
                $this->hash = new Hash($hash);
                $this->hashName = $hash;
                break;
            default:
                $this->hash = new Hash('sha1');
                $this->hashName = 'sha1';
        }
        $this->hLen = $this->hash->getLength();
    }
488932
function hash(str, algo, encoding) {
  return crypto
    .createHash(algo || 'sha1')
    .update(str)
    .digest(encoding || 'hex');
}
435385
protected String hashByteArray(byte[] data) {
        if (mCRC32 != null) {
            return hashCRC32(data);
        } else if (mHashAlgorithm != null) {
            return messageDigest(data);
        } else {
            // Default.
            return hashCRC32(data);
        }
    }
643170
public static byte[] md5Hash(String data, Charset charset) throws NoSuchAlgorithmException {
		return md5Hash(data.getBytes(charset));
	}
299767
private function dsqHmacSha1(string $data, string $key)
    {
        $blockSize = 64;
        if (strlen($key) > $blockSize) {
            $key = pack('H*', sha1($key));
        }
        $key  = str_pad($key, $blockSize, chr(0x00));
        $ipad = str_repeat(chr(0x36), $blockSize);
        $opad = str_repeat(chr(0x5c), $blockSize);
        $hmac = pack(
            'H*',
            sha1(
                ($key ^ $opad) . pack(
                    'H*',
                    sha1(
                        ($key ^ $ipad) . $data
                    )
                )
            )
        );
        return bin2hex($hmac);
    }
1314826
public function createHash($data) {
        $string = $this->recursiveHash($data);
        $secret = $this->getClientSignSecret();
        return self::base64UrlEncode(hash_hmac("sha256", $string, $secret, true));
    }
455135
protected function _sha1($data)
  {
    if (self::$_consumer_secret === null) {
      throw new CodebirdCredentialsException('To generate a hash, the consumer secret must be set.');
    }
    if (!function_exists('hash_hmac')) {
      throw new \Exception('To generate a hash, the PHP hash extension must be available.');
    }
    return base64_encode(hash_hmac(
      'sha1',
      $data,
      self::$_consumer_secret
      . '&'
      . ($this->_oauth_token_secret !== null
        ? $this->_oauth_token_secret
        : ''
      ),
      true
    ));
  }
441947
public static function verify(string $data, string $hash, string $pass): bool
    {
        // Get the salt value from the decrypted prefix
        $salt = Str::substr($hash, 0, 16);

        // Get the encrypted cost bytes out of the blob
        $chsh = Str::substr($hash, 16, 8);

        // Get the encrypted cost bytes out of the blob
        $cost = Str::substr($hash, 24, 4);

        // If the provided cost hash does not calculate to be the same as the one provided then consider the hash invalid.
        if ($chsh !== self::costHash($cost, $pass)) {
            return false;
        }

        // Decrypt the cost value stored in the 32bit int
        $cost = self::costDecrypt($cost, $salt, $pass);

        // Build a hash from the input for comparison
        $calc = self::build($data, $pass, $cost, $salt);

        // Return the boolean equivalence
        return Str::equal($hash, $calc);
    }
715305
public static function hash($data, bool $secure = false): string
    {
        return hash(self::getAlgo($secure), (string) $data);
    }
670138
protected function _hash($data, $controlType)
    {
        switch ($controlType) {
        case 'md5':
            return md5($data);
        case 'crc32':
            return crc32($data);
        case 'strlen':
            return strlen($data);
        case 'adler32':
            return hash('adler32', $data);
        default:
            Zend_Cache::throwException("Incorrect hash function : $controlType");
        }
    }
1060511
private String hash(String dataId)
   {
      try
      {
         MessageDigest digest = MessageDigest.getInstance(hashAlgorithm);
         digest.update(dataId.getBytes("UTF-8"));
         return new BigInteger(1, digest.digest()).toString(32);
      }
      catch (NumberFormatException e)
      {
         throw new RuntimeException("Could not generate the hash code of '" + dataId + "' with the algorithm '"
            + hashAlgorithm + "'", e);
      }
      catch (UnsupportedEncodingException e)
      {
         throw new RuntimeException("Could not generate the hash code of '" + dataId + "' with the algorithm '"
            + hashAlgorithm + "'", e);
      }
      catch (NoSuchAlgorithmException e)
      {
         throw new RuntimeException("Could not generate the hash code of '" + dataId + "' with the algorithm '"
            + hashAlgorithm + "'", e);
      }
   }
14686
public static byte[] md2Hash(String data, Charset charset) throws NoSuchAlgorithmException {
		return md2Hash(data.getBytes(charset));
	}
497814
public byte[] hash(byte[] data) throws SdkClientException {
        try {
            MessageDigest md =  getMessageDigestInstance();
            md.update(data);
            return md.digest();
        } catch (Exception e) {
            throw new SdkClientException(
                    "Unable to compute hash while signing request: "
                            + e.getMessage(), e);
        }
    }
605823
public static function hash($str, $hash = null)
	{
		$hash = ($hash) ? $hash : static::$hashKey;
		return hash_hmac('sha256', $str, $hash);
	}