public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } var token = JToken.Load(reader); switch (token.Type) { case JTokenType.Null: return(null); case JTokenType.String: return(new PeerId(Multibase.DecodeRaw(Multibase.Base58, (string)token))); case JTokenType.Bytes: return(new PeerId((byte[])token)); case JTokenType.Object: var value = (string)token["$value"]; return(value == null ? null : new PeerId(Multibase.DecodeRaw(Multibase.Base58, value))); default: throw new SerializationException("Unknown PeerId format"); } }
public void TestRoundTrip(MultibaseEncoding encoding) { var rand = new Random(Environment.TickCount); var buf = new byte[rand.Next(16, 256)]; rand.NextBytes(buf); var encoded = Multibase.EncodeRaw(encoding, buf); var decoded = Multibase.DecodeRaw(encoding, encoded); Assert.Equal(decoded, buf); }
public static bool TryParse(string s, MultibaseEncoding encoding, out Multihash mh) { try { var bytes = Multibase.DecodeRaw(encoding, s); mh = Decode(bytes); return(true); } catch (Exception) { } mh = null; return(false); }
private static void Process(Options options) { using (options.Source) { byte[] data = null; using (var mem = new MemoryStream()) { options.Source.CopyTo(mem); data = mem.ToArray(); } var mh = Multihash.Sum(options.Algorithm, data, options.Length); if (string.IsNullOrEmpty(options.Checksum)) { Console.WriteLine(mh.ToString(options.Encoding)); Environment.Exit(0); } var checksum = Multibase.DecodeRaw(options.Encoding, options.Checksum); if (!checksum.SequenceEqual(mh.Digest)) { if (!options.Quiet) { Console.WriteLine($"Digest mismatch, got: {Multibase.EncodeRaw(options.Encoding, mh.Digest)}, wanted: {Multibase.EncodeRaw(options.Encoding, checksum)}"); } Environment.Exit(1); } if (!options.Quiet) { Console.WriteLine("Checksum match"); } } }
public void DecodeRaw_GivenEmptyInput_ThrowsArgumentNullException(string value) { Assert.Throws <ArgumentNullException>(() => Multibase.DecodeRaw(MultibaseEncoding.Identity, value)); }
public void DecodeRaw_GivenUnknownEncoding_ThrowsUnsupportedException() { Assert.Throws <NotSupportedException>(() => Multibase.DecodeRaw((MultibaseEncoding)0x2000, "abab")); }
public static PeerId Decode(string s) { Multihash mh; return(Multihash.TryParse(s, out mh) ? new PeerId(mh) : new PeerId(Multibase.DecodeRaw <Base16Encoding>(s.ToUpper()))); }