Braintree_Digest

Digest creates an HMAC-SHA1 hash for encrypting messages.

Defined (1)

The class is defined in the following location(s).

/classes/lib/Braintree/Braintree/Digest.php  
  1. class Digest 
  2. public static function hexDigestSha1($key, $string) 
  3. if(function_exists('hash_hmac')) { 
  4. return self::_builtInHmacSha1($string, $key); 
  5. } else { 
  6. return self::_hmacSha1($string, $key); 
  7.  
  8. public static function hexDigestSha256($key, $string) 
  9. return hash_hmac('sha256', $string, hash('sha256', $key, true)); 
  10.  
  11. public static function secureCompare($left, $right) 
  12. if (strlen($left) != strlen($right)) { 
  13. return false; 
  14.  
  15. $leftBytes = unpack("C*", $left); 
  16. $rightBytes = unpack("C*", $right); 
  17.  
  18. $result = 0; 
  19. for ($i = 1; $i <= count($leftBytes); $i++) { 
  20. $result = $result | ($leftBytes[$i] ^ $rightBytes[$i]); 
  21. return $result == 0; 
  22.  
  23. public static function _builtInHmacSha1($message, $key) 
  24. return hash_hmac('sha1', $message, sha1($key, true)); 
  25.  
  26. public static function _hmacSha1($message, $key) 
  27. $pack = 'H40'; 
  28. $keyDigest = sha1($key, true); 
  29. $innerPad = str_repeat(chr(0x36), 64); 
  30. $outerPad = str_repeat(chr(0x5C), 64); 
  31.  
  32. for ($i = 0; $i < 20; $i++) { 
  33. $innerPad{$i} = $keyDigest{$i} ^ $innerPad{$i}; 
  34. $outerPad{$i} = $keyDigest{$i} ^ $outerPad{$i}; 
  35.  
  36. return sha1($outerPad.pack($pack, sha1($innerPad.$message)));