Braintree.Crypto.SecureCompare C# (CSharp) Method

SecureCompare() public method

public SecureCompare ( string left, string right ) : bool
left string
right string
return bool
        public virtual bool SecureCompare(string left, string right)
        {
            if (left == null || right == null || (left.Length != right.Length))
            {
                return false;
            }

            byte[] leftBytes = Encoding.ASCII.GetBytes(left);
            byte[] rightBytes = Encoding.ASCII.GetBytes(right);

            int result = 0;
            for (int i=0; i < leftBytes.Length; i++)
            {
                result |= leftBytes[i] ^ rightBytes[i];
            }

            return result == 0;
        }
    }

Usage Example

 private bool PayloadMatches(string signature, string payload)
 {
     var sha1Hasher = new Sha1Hasher();
     string computedSignature = sha1Hasher.HmacHash(service.PrivateKey, payload).ToLower();
     var crypto = new Crypto();
     return crypto.SecureCompare (computedSignature, signature);
 }
All Usage Examples Of Braintree.Crypto::SecureCompare