Exemple #1
0
		/**
		* Generate a new instance of an TlsMac.
		*
		* @param digest    The digest to use.
		* @param key_block A byte-array where the key for this mac is located.
		* @param offset    The number of bytes to skip, before the key starts in the buffer.
		* @param len       The length of the key.
		*/
		public TlsMac(
			IDigest	digest,
			byte[]	key_block,
			int		offset,
			int		len)
		{
			this.mac = new HMac(digest);
			KeyParameter param = new KeyParameter(key_block, offset, len);
			this.mac.Init(param);
			this.seqNo = 0;
		}
        public string CalculateHMAC(string iPut, string uName,string HMACKey)
        {
            byte[] array = Encoding.UTF8.GetBytes(iPut + "/" + uName);
            byte[] output = new byte[256];

            IDigest dig = DigestUtilities.GetDigest("SHA256");
            HMac hm = new HMac(dig);
            byte[] keyArray = Base32Encoder.Decode(HMACKey);
            hm.BlockUpdate(array, 0, array.Length);
            hm.Init(new KeyParameter(keyArray, 0, keyArray.Length));
            int len = hm.DoFinal(output, 0);
            return Base32Encoder.Encode(output);
        }
 public static string Signature(string httpmethod, string url, string parameterString, string oauthToken)
 {
     string baseString = httpmethod.ToUpper() + "&" + PercentEncode(url) + "&" + PercentEncode(parameterString);
     string signingKey = PercentEncode(consumerSecret) + "&" + PercentEncode(oauthToken);
     var sha = new HMac(new Sha1Digest());
     sha.Init(new KeyParameter(Encoding.UTF8.GetBytes(signingKey)));
     var data = Encoding.UTF8.GetBytes(baseString);
     sha.BlockUpdate(data, 0, data.Length);
     byte[] result = new byte[20];
     sha.DoFinal(result, 0);
     return Convert.ToBase64String(result);
 }
		private static void hmac_hash(IDigest digest, byte[] secret, byte[] seed, byte[] output)
		{
			HMac mac = new HMac(digest);
			KeyParameter param = new KeyParameter(secret);
			byte[] a = seed;
			int size = digest.GetDigestSize();
			int iterations = (output.Length + size - 1) / size;
			byte[] buf = new byte[mac.GetMacSize()];
			byte[] buf2 = new byte[mac.GetMacSize()];
			for (int i = 0; i < iterations; i++)
			{
				mac.Init(param);
				mac.BlockUpdate(a, 0, a.Length);
				mac.DoFinal(buf, 0);
				a = buf;
				mac.Init(param);
				mac.BlockUpdate(a, 0, a.Length);
				mac.BlockUpdate(seed, 0, seed.Length);
				mac.DoFinal(buf2, 0);
				Array.Copy(buf2, 0, output, (size * i), System.Math.Min(size, output.Length - (size * i)));
			}
		}