public void Decoding_SecondAndFirstValues_Shows_32()
    {
        var inputCharacters = ZBase32Encoder.AcceptedCharacters[1].ToString() + ZBase32Encoder.AcceptedCharacters[0];
        var result          = ZBase32Encoder.Decode(inputCharacters);

        result.ShouldEqual(32);
    }
    public void Decoding_SecondCharacter_Returns_1()
    {
        var inputCharacter = ZBase32Encoder.AcceptedCharacters[1];
        var result         = ZBase32Encoder.Decode(inputCharacter);

        result.ShouldEqual(1);
    }
        public void ConstructorDefaultTest()
        {
            ZBase32Encoder encoder = new ZBase32Encoder();

            Common.AreEqual(ZBase32Encoder.StandardAlphabet, encoder.Alphabet);
            Assert.AreEqual(ZBase32Encoder.StandardPadding, encoder.Padding);
        }
    public void Decoding_ThirdAndFirstCharacters_Shows_64()
    {
        var inputCharacters = ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[0];
        var result          = ZBase32Encoder.Decode(inputCharacters);

        result.ShouldEqual(64);
    }
Exemple #5
0
        static void Main(string[] args)
        {
            var zBase32 = new ZBase32Encoder();

            byte[] inputData = new byte[] { 0xF0, 0xBF, 0xC7 };
            string encodedText = zBase32.Encode(inputData);

            string input = null;
            do
            {
                input = Console.ReadLine();

                long number;

                if (long.TryParse(input, out number))
                {
                    //byte[] bytes = BitConverter.GetBytes((int)number);

                    string output = ZBase32.Encode(number);
                    Console.WriteLine(output);

                    long decoded = ZBase32.Decode(output); // 4242424242
                    if(decoded != number) {
                        throw new IndexOutOfRangeException();
                    }

                }
                else
                {
                    Console.WriteLine("Not a number, bigger than {0}", long.MaxValue);
                }
            } while (input != null);
        }
        public void EncodingTest(string sourceData, string encodedData)
        {
            var bytes  = Encoding.ASCII.GetBytes(sourceData);
            var result = ZBase32Encoder.Encode(bytes);

            Assert.AreEqual(encodedData, result);
        }
Exemple #7
0
        public BaseEncoder GetEncoder(EncoderTypes encoderType)
        {
            BaseEncoder encoder = null;

            switch (encoderType)
            {
                case EncoderTypes.CrockfordLower:
                    encoder = new CrockfordLowerEncoder();
                    break;
                case EncoderTypes.CrockfordMixed:
                    encoder = new CrockfordMixedEncoder();
                    break;
                case EncoderTypes.ZBase32:
                    encoder = new ZBase32Encoder();
                    break;
                case EncoderTypes.Hexadecimal:
                    encoder = new HexadecimalEncoder();
                    break;
                case EncoderTypes.UnicodeSymbols:
                    encoder = new UnicodeSymbolsEncoder();
                    break;
                default:
                    throw new ArgumentException("Encoder type not registered");
            }

            return encoder;
        }
    public void Decoding_FirstCharacter_Returns_0()
    {
        var inputCharacter = ZBase32Encoder.AcceptedCharacters[0];
        var result         = ZBase32Encoder.Decode(inputCharacter);

        result.ShouldEqual(0);
    }
        public void GetDecoderTest()
        {
            ZBase32Encoder encoder = new ZBase32Encoder();
            Decoder        decoder = encoder.GetDecoder();

            Assert.IsNotNull(decoder);
            Assert.IsInstanceOf(typeof(ZBase32Decoder), decoder);
        }
        public void PaddingTest()
        {
            ZBase32Encoder encoder     = new ZBase32Encoder();
            char           testPadding = '?';

            encoder.Padding = testPadding;
            Assert.AreEqual(testPadding, encoder.Padding);
        }
        public void AlphabetTest()
        {
            char[]         testAlphabet = new char[] { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '1', '2', '3', '4', '5', '6' };
            ZBase32Encoder encoder      = new ZBase32Encoder();

            encoder.Alphabet = testAlphabet;
            Common.AreEqual(testAlphabet, encoder.Alphabet);
        }
        public void EncodeDecodeTest(string sourceData)
        {
            var bytes       = Encoding.ASCII.GetBytes(sourceData);
            var encodedData = ZBase32Encoder.Encode(bytes);
            var decodedData = ZBase32Encoder.Decode(encodedData);

            Assert.That(Encoding.ASCII.GetBytes(sourceData), Is.EquivalentTo(decodedData));
        }
Exemple #13
0
        public static string Encode(byte[] data)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var zbase = new ZBase32Encoder();
            return zbase.Encode(data);
        }
Exemple #14
0
        public static string Get(ulong val)
        {
            if (val == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(val), $"val cannot be 0");
            }

            unchecked
            {
                val = Encode(val);
                return(ZBase32Encoder.Encode(BitConverter.GetBytes(val)));
            }
        }
        public static Guid ParseFromZBase32String(string base32)
        {
            Guard.IsNotNullNorEmpty(base32, nameof(base32));
            Guard.Against<ArgumentException>(base32.Length != ZBASE32_LENGTH,
                $"Invalid base32 string length, expected {ZBASE32_LENGTH}");

            try
            {
                var bytes = new ZBase32Encoder().Decode(base32);
                return new Guid(bytes);
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException($"Unable to parse ZBase32String '{base32}' to Guid", e);
            }
        }
Exemple #16
0
        public static Guid ParseFromZBase32String(string base32)
        {
            Guard.IsNotNullNorEmpty(base32, nameof(base32));
            Guard.Against <ArgumentException>(base32.Length != ZBASE32_LENGTH,
                                              $"Invalid base32 string length, expected {ZBASE32_LENGTH}");

            try
            {
                var bytes = new ZBase32Encoder().Decode(base32);
                return(new Guid(bytes));
            }
            catch (ArgumentException e)
            {
                throw new ArgumentException($"Unable to parse ZBase32String '{base32}' to Guid", e);
            }
        }
Exemple #17
0
        public static bool TryGetUInt64(string ocode, out ulong value)
        {
            if (!String.IsNullOrWhiteSpace(ocode))
            {
                unchecked
                {
                    var bytes = ZBase32Encoder.Decode(ocode);
                    if (bytes.Length == 8)
                    {
                        var val = BitConverter.ToUInt64(bytes, 0);
                        value = Decode(val);
                        return(true);
                    }
                }
            }

            value = default(ulong);
            return(false);
        }
Exemple #18
0
        public static ulong ToUInt64(string ocode)
        {
            if (String.IsNullOrWhiteSpace(ocode))
            {
                throw new ArgumentNullException("ocode");
            }

            unchecked
            {
                var bytes = ZBase32Encoder.Decode(ocode);
                if (bytes.Length != 8)
                {
                    throw new ArgumentException("Invalid OCode", nameof(ocode));
                }

                var val = BitConverter.ToUInt64(bytes, 0);
                return(Decode(val));
            }
        }
    public void Encoding_32_ReturnsSecondAndFirstValues()
    {
        var result = ZBase32Encoder.Encode(32);

        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[1].ToString() + ZBase32Encoder.AcceptedCharacters[0].ToString());
    }
        public void AlphabetNullTest()
        {
            ZBase32Encoder encoder = new ZBase32Encoder();

            encoder.Alphabet = null;
        }
        public void AlphabetShortTest()
        {
            ZBase32Encoder encoder = new ZBase32Encoder();

            encoder.Alphabet = new char[1];
        }
    public void Encoding_1_ReturnsSecondCharacter()
    {
        var result = ZBase32Encoder.Encode(1);

        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[1].ToString());
    }
    public void Encoding_64_ReturnsThirdAndFirstValues()
    {
        var result = ZBase32Encoder.Encode(64);

        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[0].ToString());
    }
    public void Encoding_0_ReturnsFirstCharacter()
    {
        var result = ZBase32Encoder.Encode(0);

        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[0].ToString());
    }
    public void Encoding_65_ReturnsThirdAndSecondValues()
    {
        var result = ZBase32Encoder.Encode(65);

        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[1].ToString());
    }