public string CreateToken(Dictionary <string, string> payload) { if (payload == null) { throw new ArgumentNullException("Payload is null"); } string token = ""; string tokenType = "JWT"; Dictionary <string, string> header = new Dictionary <string, string>(); header.Add("alg", "SHA256"); header.Add("typ", tokenType); string headerEncode = _encoder.Encode(header); string payloadEncode = _encoder.Encode(payload); string signEncode = GenerateTokenSignature(headerEncode, payloadEncode, key); Console.Out.WriteLine($"Header: {headerEncode} Payload: {payloadEncode}\n"); token = $"{headerEncode}.{payloadEncode}.{signEncode}"; return(token); }
private SqrlMessage PrepareMessage(Uri uri) { string site = uri.Host; int additionalDomainsChars = GetAdditionalDomainChars(uri); if (additionalDomainsChars > 1 && additionalDomainsChars <= uri.LocalPath.Length) { site += uri.LocalPath.Substring(0, additionalDomainsChars); } byte[] siteKey = _identity.GetSitePrivateKey(site); var keys = CryptoSign.GenerateKeyPair(siteKey); var message = new SqrlMessage { PublicKeyBase64 = Convert.ToBase64String(keys.PublicKey) }; StringBuilder url = new StringBuilder(uri.ToString()); if (string.IsNullOrEmpty(uri.Query)) { url.Append("?"); } else if (!uri.Query.EndsWith("?") && !uri.Query.EndsWith("&")) { url.Append("&"); } url.Append("sqrlver=").Append(SqrlVersion); url.Append("&sqrlkey=").Append(UrlSafeBase64Encoder.Encode(message.PublicKeyBase64)); message.Uri = new Uri(url.ToString()); var idn = new IdnMapping(); var puny = idn.GetAscii(message.Uri.ToString()); byte[] urlBytes = Encoding.ASCII.GetBytes(puny); byte[] signed = CryptoSign.Sign(urlBytes, keys.SecretKey); // The signature is only the first 64 bytes, the rest is the message Array.Resize(ref signed, 64); message.SignatureBase64 = Convert.ToBase64String(signed); return(message); }
private void SendMessage(SqrlMessage message) { string url = message.Uri.ToString(); url = message.Uri.Scheme == SchemeSqrls ? url.Replace("sqrl://", "https://") : url.Replace("qrl://", "http://"); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("sqrlsig", UrlSafeBase64Encoder.Encode(message.SignatureBase64)) }); using (var client = new HttpClient()) { var result = client.PostAsync(url, content).Result; result.EnsureSuccessStatusCode(); } }