Пример #1
0
        public static string HMACSign(string data, System.Security.SecureString key, KeyedHashAlgorithm algorithm)
#endif
        {
#if UNITY
            if (String.IsNullOrEmpty(key))
#else
            if (null == key)
#endif
            {
                throw new ArgumentNullException("key", "The AWS Secret Access Key specified is NULL!");
            }

            if (String.IsNullOrEmpty(data))
            {
                throw new ArgumentNullException("data", "Please specify data to sign.");
            }

            if (null == algorithm)
            {
                throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
            }
#if UNITY
            try
            {
                algorithm.Key = Encoding.UTF8.GetBytes(key);
                return Convert.ToBase64String(algorithm.ComputeHash(
                    Encoding.UTF8.GetBytes(data.ToCharArray()))
                    );
            }
            finally
            {
                algorithm.Clear();
            }
#else
            // pointer to hold unmanaged reference to SecureString instance
            IntPtr bstr = IntPtr.Zero;
            char[] charArray = new char[key.Length];
            try
            {
                // Marshal SecureString into byte array
                bstr = Marshal.SecureStringToBSTR(key);
                Marshal.Copy(bstr, charArray, 0, charArray.Length);
                algorithm.Key = Encoding.UTF8.GetBytes(charArray);
                return Convert.ToBase64String(algorithm.ComputeHash(
                    Encoding.UTF8.GetBytes(data.ToCharArray()))
                    );
            }
            finally
            {
                // Make sure that the clear text data is zeroed out
                Marshal.ZeroFreeBSTR(bstr);
                algorithm.Clear();
                Array.Clear(charArray, 0, charArray.Length);
            }
#endif
        }
Пример #2
0
        /// <summary>
        /// Computes RFC 2104-compliant HMAC signature
        /// </summary>
        /// <param name="data">The data to be signed</param>
        /// <param name="key">The secret signing key</param>
        /// <param name="algorithm">The algorithm to sign the data with</param>
        /// <exception cref="T:System.ArgumentNullException"/>
        /// <returns>A string representing the HMAC signature</returns>
        public static string HMACSign(byte[] data, string key, KeyedHashAlgorithm algorithm)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
            }

            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data", "Please specify data to sign.");
            }

            if (null == algorithm)
            {
                throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
            }

            try
            {
                algorithm.Key = Encoding.UTF8.GetBytes(key);
                byte[] bytes = algorithm.ComputeHash(data);
                return Convert.ToBase64String(bytes);
            }
            finally
            {
                algorithm.Clear();
            }
        }
Пример #3
0
 public static string HMACSign(string data, SecureString key, KeyedHashAlgorithm algorithm)
 {
     string str;
     if (key == null)
     {
         throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
     }
     if (string.IsNullOrEmpty(data))
     {
         throw new ArgumentNullException("data", "Please specify data to sign.");
     }
     if (algorithm == null)
     {
         throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
     }
     IntPtr zero = IntPtr.Zero;
     char[] destination = new char[key.Length];
     try
     {
         zero = Marshal.SecureStringToBSTR(key);
         Marshal.Copy(zero, destination, 0, destination.Length);
         algorithm.Key = Encoding.UTF8.GetBytes(destination);
         str = Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(data.ToCharArray())));
     }
     finally
     {
         Marshal.ZeroFreeBSTR(zero);
         algorithm.Clear();
         Array.Clear(destination, 0, destination.Length);
     }
     return str;
 }
Пример #4
0
 public static string HMACSign(string data, string key, KeyedHashAlgorithm algorithm)
 {
     string str;
     if (string.IsNullOrEmpty(key))
     {
         throw new ArgumentNullException("key", "Please specify a Secret Signing Key.");
     }
     if (string.IsNullOrEmpty(data))
     {
         throw new ArgumentNullException("data", "Please specify data to sign.");
     }
     if (algorithm == null)
     {
         throw new ArgumentNullException("algorithm", "Please specify a KeyedHashAlgorithm to use.");
     }
     try
     {
         algorithm.Key = Encoding.UTF8.GetBytes(key);
         str = Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(data)));
     }
     finally
     {
         algorithm.Clear();
     }
     return str;
 }