Skip to content

Commit

Permalink
feat(MultiBase): implement base32z
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Apr 30, 2018
1 parent 96b8656 commit a0565d3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Base32z.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SimpleBase;
using System;
using System.Collections.Generic;
using System.Text;

namespace Ipfs
{
/// <summary>
/// Base32 encoding designed to be easier for human use and more compact.
/// </summary>
/// <remarks>
/// Commonly referred to as 'z-base-32'.
/// </remarks>
/// <seealso href="https://en.wikipedia.org/wiki/Base32#z-base-32"/>
public static class Base32z
{
static readonly Base32Alphabet alphabet =
new Base32Alphabet("ybndrfg8ejkmcpqxot1uwisza345h769");

/// <summary>
/// The encoder/decoder for z-base-32.
/// </summary>
public static readonly SimpleBase.Base32 Codec = new SimpleBase.Base32(alphabet);
}
}
7 changes: 4 additions & 3 deletions src/Registry/MultiBaseAlgorithm .cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Ipfs.Registry
/// the currently defined multi-base algorithms.
/// <para>
/// These algorithms are supported: base58btc, base58flickr, base64,
/// base64pad, base64url, base16, base32, base32pad, base32hex
/// base64pad, base64url, base16, base32, base32z, base32pad, base32hex
/// and base32hexpad.
/// </para>
/// </remarks>
Expand Down Expand Up @@ -73,14 +73,15 @@ static MultiBaseAlgorithm()
Register("BASE32HEXPAD", 'T',
bytes => SimpleBase.Base32.ExtendedHex.Encode(bytes, true),
s => SimpleBase.Base32.ExtendedHex.Decode(s));

Register("base32z", 'h',
bytes => Base32z.Codec.Encode(bytes, false),
s => Base32z.Codec.Decode(s));
// Not supported
#if false
Register("base1", '1');
Register("base2", '0');
Register("base8", '7');
Register("base10", '9');
Register("base32z", 'h');
#endif
}

Expand Down
20 changes: 20 additions & 0 deletions test/MultBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ class TestVector
Input = "f",
Output = "TCO======"
},
new TestVector {
Algorithm = "base32z",
Input="Decentralize everything!!",
Output ="het1sg3mqqt3gn5djxj11y3msci3817depfzgqejb"
},
new TestVector {
Algorithm = "base32z",
Input ="yes mani !",
Output ="hxf1zgedpcfzg1ebb"
},
new TestVector {
Algorithm = "base32z",
Input ="hello world",
Output ="hpb1sa5dxrb5s6hucco"
},
new TestVector {
Algorithm = "base32z",
Input ="\x00\x00yes mani !",
Output ="hyyy813murbssn5ujryoo"
},

};

Expand Down

0 comments on commit a0565d3

Please sign in to comment.