diff --git a/SimplePack/SimplePack.cs b/SimplePack/SimplePack.cs new file mode 100644 index 0000000..64ffbc0 --- /dev/null +++ b/SimplePack/SimplePack.cs @@ -0,0 +1,35 @@ +using System.Collections; +using Base58Check; + +namespace simplepack +{ + + public class SimplePack + { + private string head; + private string foot; + public SimplePack(string header, string footer){ + head = header; + foot = footer; + if (header.Length == 0 | footer.Length == 0){ + throw new InvalidSimplePackHeader(); + } + } + + public byte[] decode(string data){ + if (! data.Contains(head) | ! data.Contains(foot)){ throw new InvalidSimplePackHeader();} + + string base58Data = ""; + for (int i = head.Length; i < data.Length - foot.Length; i++){ + base58Data += data[i]; + } + return Base58CheckEncoding.Decode(base58Data); + } + + public string encode(byte[] data){ + // OutOfMemoryException may be thrown for large payloads + return head + Base58CheckEncoding.Encode(data) + foot; + } + + } +} diff --git a/SimplePack/SimplePack.csproj b/SimplePack/SimplePack.csproj new file mode 100644 index 0000000..aa21527 --- /dev/null +++ b/SimplePack/SimplePack.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/SimplePack/exception.cs b/SimplePack/exception.cs new file mode 100644 index 0000000..d53eccc --- /dev/null +++ b/SimplePack/exception.cs @@ -0,0 +1,22 @@ +using System; + + +namespace simplepack{ + public class InvalidSimplePackHeader : Exception + { + public InvalidSimplePackHeader() + { + } + + public InvalidSimplePackHeader(string message) + : base(message) + { + } + + public InvalidSimplePackHeader(string message, Exception inner) + : base(message, inner) + { + } + } +} + diff --git a/cli/SimplePackCLI.cs b/cli/SimplePackCLI.cs new file mode 100644 index 0000000..4963df3 --- /dev/null +++ b/cli/SimplePackCLI.cs @@ -0,0 +1,12 @@ +using System; + +namespace cli +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/cli/cli.csproj b/cli/cli.csproj new file mode 100644 index 0000000..50a97e7 --- /dev/null +++ b/cli/cli.csproj @@ -0,0 +1,12 @@ + + + + + + + + Exe + netcoreapp3.1 + + + diff --git a/tests/testSimplePack.cs b/tests/testSimplePack.cs new file mode 100644 index 0000000..a459953 --- /dev/null +++ b/tests/testSimplePack.cs @@ -0,0 +1,85 @@ +using NUnit.Framework; +using System.Text; +using Base58Check; +using simplepack; + + +namespace tests +{ + public class Tests + { + [SetUp] + public void Setup() + { + } + + [Test] + public void TestRequireHeader(){ + string header = ""; + string footer = " test-footer"; + byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); + string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer; + try{ + SimplePack packer = new SimplePack(header, footer); + } + catch(InvalidSimplePackHeader){ + return; + } + Assert.Fail(); + } + + [Test] + public void TestRequireFooter(){ + string header = "test-header"; + string footer = ""; + byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); + string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer; + try{ + SimplePack packer = new SimplePack(header, footer); + } + catch(InvalidSimplePackHeader){ + return; + } + Assert.Fail(); + } + + [Test] + public void TestInvalidChecksum(){ + byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); + string header = "header-part "; + string footer = " footer part."; + string encoded = header + Base58CheckEncoding.EncodePlain(expected_result) + footer; + SimplePack packer = new SimplePack(header, footer); + try{ + packer.decode(encoded); + } + catch(System.FormatException){return;} + Assert.Fail(); // Did not catch no checksum, fail + + } + + [Test] + public void TestDecode() + { + byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world"); + string header = "header-part "; + string footer = " footer part."; + string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer; + SimplePack packer = new SimplePack(header, footer); + Assert.AreEqual(packer.decode(encoded), + expected_result); + } + + [Test] + public void TestEncode() + { + byte[] message = UTF8Encoding.UTF8.GetBytes("hello world"); + string expected_header = "header-part "; + string expected_footer = " footer part."; + string expected_result = expected_header + Base58CheckEncoding.Encode(message) + expected_footer; + SimplePack packer = new SimplePack(expected_header, expected_footer); + string actual_result = packer.encode(message); + Assert.AreEqual(actual_result, expected_result); + } + } +} \ No newline at end of file diff --git a/tests/tests.csproj b/tests/tests.csproj new file mode 100644 index 0000000..971e5ec --- /dev/null +++ b/tests/tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + +