示例#1
0
        /// <summary>
        ///   Returns a string representation of the CID
        ///   according to the provided format specifier.
        /// </summary>
        /// <param name="format">
        ///   A single format specifier that indicates how to format the value of the
        ///   CID.  Can be "G" or "L".
        /// </param>
        /// <returns>
        ///   The CID in the specified <paramref name="format"/>.
        /// </returns>
        /// <exception cref="FormatException">
        ///   <paramref name="format"/> is not valid.
        /// </exception>
        /// <remarks>
        /// <para>
        ///   The "G" format specifier is the same as calling <see cref="Encode"/>.
        /// </para>
        /// <list type="table">
        /// <listheader>
        ///   <term>Specifier</term>
        ///   <description>return value</description>
        /// </listheader>
        ///  <item>
        ///    <term>G</term>
        ///    <description>QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V</description>
        ///  </item>
        ///  <item>
        ///    <term>L</term>
        ///    <description>base58btc cidv0 dag-pb sha2-256 Qm...</description>
        ///  </item>
        /// </list>
        /// </remarks>
        public string ToString(string format)
        {
            switch (format)
            {
            case "G":
                return(Encode());

            case "L":
                var sb = new StringBuilder();
                sb.Append(Encoding);
                sb.Append(' ');
                sb.Append("cidv");
                sb.Append(Version);
                sb.Append(' ');
                sb.Append(ContentType);
                if (Hash != null)
                {
                    sb.Append(' ');
                    sb.Append(Hash.Algorithm.Name);
                    sb.Append(' ');
                    sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
                }
                return(sb.ToString());

            default:
                throw new FormatException($"Invalid CID format specifier '{format}'.");
            }
        }
示例#2
0
        public void Codec()
        {
            var bytes  = new byte[] { 1, 2, 3, 4, 5 };
            var bytes1 = MultiBase.Decode(MultiBase.Encode(bytes));
            var bytes2 = MultiBase.Decode(MultiBase.Encode(bytes, "base16"));

            CollectionAssert.AreEqual(bytes, bytes1);
            CollectionAssert.AreEqual(bytes, bytes2);
        }
示例#3
0
        public void EmptyData()
        {
            var empty = new byte[0];

            foreach (var alg in MultiBaseAlgorithm.All)
            {
                var s = MultiBase.Encode(empty, alg.Name);
                CollectionAssert.AreEqual(empty, MultiBase.Decode(s), alg.Name);
            }
        }
示例#4
0
 public void CheckMultiBase()
 {
     foreach (var v in TestVectors)
     {
         var bytes = Encoding.UTF8.GetBytes(v.Input);
         var s     = MultiBase.Encode(bytes, v.Algorithm);
         Assert.AreEqual(v.Output, s);
         CollectionAssert.AreEqual(bytes, MultiBase.Decode(s));
     }
 }
示例#5
0
        /// <summary>
        ///   Converts the CID to its equivalent string representation.
        /// </summary>
        /// <returns>
        ///   The string representation of the <see cref="Cid"/>.
        /// </returns>
        /// <remarks>
        ///   For <see cref="Version"/> 0, this is equalivalent to the
        ///   <see cref="MultiHash.ToBase58()">base58btc encoding</see>
        ///   of the <see cref="Hash"/>.
        /// </remarks>
        /// <seealso cref="Decode"/>
        public string Encode()
        {
            if (Version == 0)
            {
                return(Hash.ToBase58());
            }

            using (var ms = new MemoryStream())
            {
                ms.WriteVarint(Version);
                ms.WriteMultiCodec(ContentType);
                Hash.Write(ms);
                return(MultiBase.Encode(ms.ToArray(), Encoding));
            }
        }
示例#6
0
 /// <summary>
 ///   A CID that is readable by a human.
 /// </summary>
 /// <returns>
 ///  e.g. "base58btc cidv0 dag-pb sha2-256 Qm..."
 /// </returns>
 public override string ToString()
 {
     var sb = new StringBuilder();
     sb.Append(Encoding);
     sb.Append(' ');
     sb.Append("cidv");
     sb.Append(Version);
     sb.Append(' ');
     sb.Append(ContentType);
     if (Hash != null)
     {
         sb.Append(' ');
         sb.Append(Hash.Algorithm.Name);
         sb.Append(' ');
         sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
     }
     return sb.ToString();
 }
示例#7
0
        /// <summary>
        ///   Converts the CID to its equivalent string representation.
        /// </summary>
        /// <returns>
        ///   The string representation of the <see cref="Cid"/>.
        /// </returns>
        /// <remarks>
        ///   For <see cref="Version"/> 0, this is equalivalent to the 
        ///   <see cref="MultiHash.ToBase58()">base58btc encoding</see>
        ///   of the <see cref="Hash"/>.
        /// </remarks>
        /// <seealso cref="Decode"/>
        public string Encode()
        {
            if (encodedValue != null)
                return encodedValue;

            if (Version == 0)
            {
                encodedValue = Hash.ToBase58();
            }
            else
            {
                using (var ms = new MemoryStream())
                {
                    ms.WriteVarint(Version);
                    ms.WriteMultiCodec(ContentType);
                    Hash.Write(ms);
                    encodedValue = MultiBase.Encode(ms.ToArray(), Encoding);
                }
            }
            return encodedValue;
        }
示例#8
0
 public void Encode_Null_Data_Not_Allowed()
 {
     ExceptionAssert.Throws <ArgumentNullException>(() => MultiBase.Encode(null));
 }
示例#9
0
        public void Encode_Unknown_Algorithm()
        {
            var bytes = new byte[] { 1, 2, 3, 4, 5 };

            ExceptionAssert.Throws <KeyNotFoundException>(() => MultiBase.Encode(bytes, "unknown"));
        }