Exemple #1
0
        public void Decode_GivenInvalidChars_ThrowsInvalidOperationException()
        {
            // prefix 0 - base2
            // value 99 - invalid chars

            Assert.Throws <InvalidOperationException>(() => Multibase.Decode("099", out string _));
        }
Exemple #2
0
        public static Key LoadNBEcKey(String base58)
        {
            byte[] b   = Multibase.Decode(base58, out MultibaseEncoding encoding);
            EcKey  key = EcKey.FromAsn1(b);

            return(new Key(key.GetPrivKeyBytes()));
        }
Exemple #3
0
        private ulong StringToInt(string value)
        {
            byte[] codedBytes = Multibase.Decode("Z" + value, out MultibaseEncoding _);
            ulong  codedValue = BitConverter.ToUInt64(codedBytes, 0);

            return(Rearrange(codedValue));
        }
Exemple #4
0
        public void TestVector_6(string encoding, string encoded)
        {
            var expected = "hello world";

            var decoded = Multibase.Decode(encoded, out string mbEncoding, false);

            Assert.Equal(encoding, mbEncoding);
            Assert.Equal(expected, Encoding.UTF8.GetString(decoded));
        }
Exemple #5
0
        // Official test vectors
        private static void TestVector(string encoding, string encoded, string expected)
        {
            var decoded = Multibase.Decode(encoded, out string mbEncoding);

            Assert.Equal(encoding, mbEncoding);
            Assert.Equal(expected, Encoding.UTF8.GetString(decoded));

            var rencoded = Multibase.Encode(mbEncoding, decoded);

            Assert.Equal(encoded, rencoded);
        }
Exemple #6
0
        public void TestRoundTripRaw(MultibaseEncoding encoding)
        {
            var rand = new Random(Environment.TickCount);
            var buf  = new byte[rand.Next(16, 256)];

            rand.NextBytes(buf);

            var encoded = Multibase.Encode(encoding, buf);
            var decoded = Multibase.Decode(encoded, out MultibaseEncoding decodedEncoding);

            Assert.Equal(encoding, decodedEncoding);
            Assert.Equal(decoded, buf);
        }
Exemple #7
0
        public static Cid Decode(string v)
        {
            if (v.Length < 2)
            {
                throw new ArgumentException("cid too short", nameof(v));
            }

            if (v.Length == 46 && v.Substring(0, 2) == "Qm")
            {
                return(new Cid(Multihash.Parse(v)));
            }

            return(Cast(Multibase.Decode(v)));
        }
Exemple #8
0
        public static ApiSig CreateApiSig(string secret, DateTime?date = null)
        {
            if (date is null)
            {
                date = DateTime.UtcNow.AddMinutes(30);
            }

            byte[] sec = Multibase.Decode(secret, out string _);
            string msg = date.Value.ToString("o", CultureInfo.InvariantCulture);

            using HMACSHA256 hash = new (sec);
            byte[] mac = hash.ComputeHash(Encoding.ASCII.GetBytes(msg));
            string sig = Multibase.Encode(MultibaseEncoding.Base32Lower, mac);

            return(new ApiSig()
            {
                Msg = msg,
                Sig = sig
            });
        }
Exemple #9
0
 public static PublicKey FromString(string str)
 {
     byte[] decoded = Multibase.Decode(str, out string _);
     return(new PublicKey(LibP2P.Crypto.PublicKey.Unmarshal(decoded)));
 }
Exemple #10
0
 public void Decode_GivenUnknownPrefix_ThrowsUnsupportedException()
 {
     Assert.Throws <NotSupportedException>(() => Multibase.Decode("ø", out string _));
 }
Exemple #11
0
 public void Decode_GivenEmptyInput_ThrowsArgumentNullException(string value)
 {
     Assert.Throws <ArgumentNullException>(() => Multibase.Decode(value, out string _));
 }
Exemple #12
0
 public override void Decode(string value) => Value = Multibase.Decode(value, out MultibaseEncoding _);