From a0565d3d8527f7297d64fd936e3ce2ee889e313d Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 30 Apr 2018 14:06:22 +1200 Subject: [PATCH] feat(MultiBase): implement base32z --- src/Base32z.cs | 25 +++++++++++++++++++++++++ src/Registry/MultiBaseAlgorithm .cs | 7 ++++--- test/MultBaseTest.cs | 20 ++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/Base32z.cs diff --git a/src/Base32z.cs b/src/Base32z.cs new file mode 100644 index 0000000..b405d7f --- /dev/null +++ b/src/Base32z.cs @@ -0,0 +1,25 @@ +using SimpleBase; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Ipfs +{ + /// + /// Base32 encoding designed to be easier for human use and more compact. + /// + /// + /// Commonly referred to as 'z-base-32'. + /// + /// + public static class Base32z + { + static readonly Base32Alphabet alphabet = + new Base32Alphabet("ybndrfg8ejkmcpqxot1uwisza345h769"); + + /// + /// The encoder/decoder for z-base-32. + /// + public static readonly SimpleBase.Base32 Codec = new SimpleBase.Base32(alphabet); + } +} diff --git a/src/Registry/MultiBaseAlgorithm .cs b/src/Registry/MultiBaseAlgorithm .cs index 783ffb2..aa767bd 100644 --- a/src/Registry/MultiBaseAlgorithm .cs +++ b/src/Registry/MultiBaseAlgorithm .cs @@ -13,7 +13,7 @@ namespace Ipfs.Registry /// the currently defined multi-base algorithms. /// /// These algorithms are supported: base58btc, base58flickr, base64, - /// base64pad, base64url, base16, base32, base32pad, base32hex + /// base64pad, base64url, base16, base32, base32z, base32pad, base32hex /// and base32hexpad. /// /// @@ -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 } diff --git a/test/MultBaseTest.cs b/test/MultBaseTest.cs index 55dab02..367c0a7 100644 --- a/test/MultBaseTest.cs +++ b/test/MultBaseTest.cs @@ -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" + }, };