/// <exception cref="System.IO.IOException"></exception> private void Authorize(HttpURLConnection c) { IDictionary <string, IList <string> > reqHdr = c.GetRequestProperties(); SortedDictionary <string, string> sigHdr = new SortedDictionary <string, string>(); foreach (KeyValuePair <string, IList <string> > entry in reqHdr.EntrySet()) { string hdr = entry.Key; if (IsSignedHeader(hdr)) { sigHdr.Put(StringUtils.ToLowerCase(hdr), ToCleanString(entry.Value)); } } StringBuilder s = new StringBuilder(); s.Append(c.GetRequestMethod()); s.Append('\n'); s.Append(Remove(sigHdr, "content-md5")); s.Append('\n'); s.Append(Remove(sigHdr, "content-type")); s.Append('\n'); s.Append(Remove(sigHdr, "date")); s.Append('\n'); foreach (KeyValuePair <string, string> e in sigHdr.EntrySet()) { s.Append(e.Key); s.Append(':'); s.Append(e.Value); s.Append('\n'); } string host = c.GetURL().GetHost(); s.Append('/'); s.Append(Sharpen.Runtime.Substring(host, 0, host.Length - DOMAIN.Length - 1)); s.Append(c.GetURL().AbsolutePath); string sec; try { Mac m = Mac.GetInstance(HMAC); m.Init(privateKey); sec = Base64.EncodeBytes(m.DoFinal(Sharpen.Runtime.GetBytesForString(s.ToString() , "UTF-8"))); } catch (NoSuchAlgorithmException e_1) { throw new IOException(MessageFormat.Format(JGitText.Get().noHMACsupport, HMAC, e_1 .Message)); } catch (InvalidKeyException e_1) { throw new IOException(MessageFormat.Format(JGitText.Get().invalidKey, e_1.Message )); } c.SetRequestProperty("Authorization", "AWS " + publicKey + ":" + sec); }
/// <exception cref="System.Exception"></exception> public virtual void Init(byte[] key) { if (key.Length > bsize) { byte[] tmp = new byte[bsize]; System.Array.Copy(key, 0, tmp, 0, bsize); key = tmp; } SecretKeySpec skey = new SecretKeySpec(key, "HmacSHA1"); mac = Mac.GetInstance("HmacSHA1"); mac.Init(skey); }
/// <exception cref="System.Exception"></exception> public virtual void Init(byte[] key) { if (key.Length > 16) { byte[] tmp = new byte[16]; System.Array.Copy(key, 0, tmp, 0, 16); key = tmp; } SecretKeySpec skey = new SecretKeySpec(key, "HmacMD5"); mac = Mac.GetInstance("HmacMD5"); mac.Init(skey); }
/// <summary> /// Returns the keyed hash algorithm from the platform. /// </summary> /// <param name="algorithm">The algorithm desired.</param> /// <returns>The platform-specific algorithm.</returns> internal static Mac GetAlgorithm(MacAlgorithm algorithm) { string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm); try { return(Mac.GetInstance(algorithmName)); } catch (Java.Security.NoSuchAlgorithmException ex) { throw new NotSupportedException(ex.Message, ex); } }
public static Mac GetMac(HashAlgorithm hashAlgorithm) { try { if (hashAlgorithm.needsBouncyCastle) { registerBouncyCastle(); return(Mac.GetInstance(hashAlgorithm.jceHmacId, "BC")); } else { return(Mac.GetInstance(hashAlgorithm.jceHmacId)); } } catch (Exception e) { throw new EncryptedDocumentException("hmac algo not supported", e); } }
/** * Example code from http://msdn.microsoft.com/library/azure/dn495627.aspx to * construct a SaS token from the access key to authenticate a request. * * @param uri The unencoded resource URI string for this operation. The resource * URI is the full URI of the Service Bus resource to which access is * claimed. For example, * "http://<namespace>.servicebus.windows.net/<hubName>" */ private static String GenerateSasToken(String uri) { String targetUri; String token = null; try { targetUri = new String(URLEncoder .Encode(uri.ToString().ToLower(), "UTF-8") .ToLower()); long expiresOnDate = JavaSystem.CurrentTimeMillis(); int expiresInMins = 60; // 1 hour expiresOnDate += expiresInMins * 60 * 1000; long expires = expiresOnDate / 1000; String toSign = new String(targetUri + "\n" + expires); // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = HubSasKeyValue.GetBytes("UTF-8"); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.GetInstance("HmacSHA256"); mac.Init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.DoFinal(toSign.GetBytes("UTF-8")); // Using android.util.Base64 for Android Studio instead of // Apache commons codec String signature = new String(URLEncoder.Encode( Base64.EncodeToString(rawHmac, Base64Flags.NoWrap).ToString(), "UTF-8")); // Construct authorization string token = new String("SharedAccessSignature sr=" + targetUri + "&sig=" + signature + "&se=" + expires + "&skn=" + HubSasKeyName); } catch (Exception e) { MainActivity.CurrentActivity.ToastNotify("Exception Generating SaS : " + e.Message.ToString()); //if (isVisible) //{ // ToastNotify("Exception Generating SaS : " + e.getMessage().toString()); //} } return(token); }
/// <summary> /// Returns the keyed hash algorithm from the platform. /// </summary> /// <param name="algorithm">The algorithm desired.</param> /// <returns>The platform-specific algorithm.</returns> internal static Mac GetAlgorithm(MacAlgorithm algorithm) { string algorithmName = MacAlgorithmProviderFactory.GetAlgorithmName(algorithm); return(Mac.GetInstance(algorithmName)); }