Esempio n. 1
0
        private byte[] ValidateHmac(byte[] encryptedData)
        {
            byte[] validatedHmac = null;

            if (encryptedData != null)
            {
                var hmacByteLength = Options.HmacSize / 8;

                if (encryptedData.Length > hmacByteLength)
                {
                    var startIndex = encryptedData.Length - hmacByteLength;
                    var dataLength = encryptedData.Length - hmacByteLength;
                    var hmac       = new byte[hmacByteLength];
                    var hmacData   = new byte[dataLength];

                    Buffer.BlockCopy(encryptedData, 0, hmacData, 0, dataLength);
                    Buffer.BlockCopy(encryptedData, startIndex, hmac, 0, hmacByteLength);

                    var isValid = HmacProvider.ValidateHmac(Options.HmacKey, hmacData, hmac);

                    if (isValid)
                    {
                        validatedHmac = hmacData;
                    }
                }
            }

            return(validatedHmac);
        }
Esempio n. 2
0
        private byte[] HmacEncryptedData(byte[] encryptedData)
        {
            byte[] encryptedHmac = null;

            if (encryptedData != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    memoryStream.Write(encryptedData, 0, encryptedData.Length);

                    var hmac = HmacProvider.CalculateHash(Options.HmacKey, memoryStream.ToArray());

                    if (hmac != null)
                    {
                        memoryStream.Write(hmac, 0, hmac.Length);
                        encryptedHmac = memoryStream.ToArray();
                    }
                }
            }

            return(encryptedHmac);
        }