public void ReturnsTheSignatureBytes()
            {
                var expected = Convert.FromBase64String(SampleData.Signature);
                var msg = new SqrlMessage {SignatureBase64 = SampleData.Signature};

                byte[] actual = msg.SignatureBytes;

                actual.Should().BeEquivalentTo(expected);
            }
            public void ReturnsThePublicKeyBytes()
            {
                var expected = Convert.FromBase64String(SampleData.PublicKey);
                var msg = new SqrlMessage {PublicKeyBase64 = SampleData.PublicKey};

                byte[] actual = msg.PublicKeyBytes;

                actual.Should().BeEquivalentTo(expected);
            }
Exemple #3
0
            public void ReturnsThePublicKeyBytes()
            {
                var expected = Convert.FromBase64String(SampleData.PublicKey);
                var msg      = new SqrlMessage {
                    PublicKeyBase64 = SampleData.PublicKey
                };

                byte[] actual = msg.PublicKeyBytes;

                actual.Should().BeEquivalentTo(expected);
            }
Exemple #4
0
            public void ReturnsTheSignatureBytes()
            {
                var expected = Convert.FromBase64String(SampleData.Signature);
                var msg      = new SqrlMessage {
                    SignatureBase64 = SampleData.Signature
                };

                byte[] actual = msg.SignatureBytes;

                actual.Should().BeEquivalentTo(expected);
            }
Exemple #5
0
        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;
        }
Exemple #6
0
        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();
            }
        }