-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
Issues/20 Create data encoder/decoder
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using NUnit.Framework; | ||
using Unicache.Plugin; | ||
|
||
namespace Unicache.Test.Plugin | ||
{ | ||
public class XorEncoderDecoderTest | ||
{ | ||
private IUnicache cache; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
this.cache = new MemoryCache(); | ||
this.cache.CacheLocator = new SimpleCacheLocator(); | ||
this.cache.Clear(); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
this.cache.Clear(); | ||
} | ||
|
||
[Test] | ||
public void EncodeTest() | ||
{ | ||
var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); | ||
Assert.AreEqual(new byte[] { }, encoderDecoder.Encode(new byte[] { })); | ||
Assert.AreEqual(new byte[] {0x01}, encoderDecoder.Encode(new byte[] {0x00})); | ||
Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Encode(new byte[] {0x01})); | ||
Assert.AreEqual(new byte[] {0xFE, 0x00}, encoderDecoder.Encode(new byte[] {0xFF, 0x01})); | ||
} | ||
|
||
[Test] | ||
public void DecodeTest() | ||
{ | ||
var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); | ||
Assert.AreEqual(new byte[] { }, encoderDecoder.Decode(new byte[] { })); | ||
Assert.AreEqual(new byte[] {0x01}, encoderDecoder.Decode(new byte[] {0x00})); | ||
Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Decode(new byte[] {0x01})); | ||
Assert.AreEqual(new byte[] {0xFE, 0x00}, encoderDecoder.Decode(new byte[] {0xFF, 0x01})); | ||
} | ||
|
||
[Test] | ||
public void EncodeDecodeTest() | ||
{ | ||
var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); | ||
Assert.AreEqual(new byte[] { }, encoderDecoder.Decode(encoderDecoder.Encode(new byte[] { }))); | ||
Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Decode(encoderDecoder.Encode(new byte[] {0x00}))); | ||
Assert.AreEqual(new byte[] {0xFF, 0x01}, | ||
encoderDecoder.Decode(encoderDecoder.Encode(new byte[] {0xFF, 0x01}))); | ||
} | ||
|
||
[Test] | ||
public void CacheEncodeTest() | ||
{ | ||
var encoderDecoder = new XorEncoderDecoder("abc"); | ||
this.cache.Encoder = encoderDecoder; | ||
this.cache.Decoder = encoderDecoder; | ||
this.cache.SetCache("foo", System.Text.UTF8Encoding.UTF8.GetBytes("foobar")); | ||
Assert.AreEqual(System.Text.UTF8Encoding.UTF8.GetBytes("foobar"), this.cache.GetCache("foo")); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Unicache | ||
{ | ||
public interface ICacheDecoder | ||
{ | ||
byte[] Decode(byte[] data); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Unicache | ||
{ | ||
public interface ICacheEncoder | ||
{ | ||
byte[] Encode(byte[] data); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Unicache | ||
{ | ||
public interface ICacheEncoderDecoder : ICacheEncoder, ICacheDecoder | ||
{ | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Unicache.Plugin | ||
{ | ||
public class VoidEncoderDecoder : ICacheEncoderDecoder | ||
{ | ||
public byte[] Decode(byte[] data) | ||
{ | ||
return data; | ||
} | ||
|
||
public byte[] Encode(byte[] data) | ||
{ | ||
return data; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
|
||
namespace Unicache.Plugin | ||
{ | ||
public class XorEncoderDecoder : ICacheEncoderDecoder | ||
{ | ||
private byte[] Key; | ||
|
||
public XorEncoderDecoder(string key) : this(System.Text.UTF8Encoding.UTF8.GetBytes(key)) | ||
{ | ||
} | ||
|
||
public XorEncoderDecoder(byte[] key) | ||
{ | ||
if (key == null || key.Length < 1) | ||
{ | ||
throw new ArgumentException("key length must be greater than 0"); | ||
} | ||
|
||
this.Key = key; | ||
} | ||
|
||
private byte[] CreateKey(byte[] data) | ||
{ | ||
byte[] dataKey = new byte[data.Length]; | ||
for (int i = 0; i < data.Length; i++) | ||
{ | ||
dataKey[i] = this.Key[i % this.Key.Length]; | ||
} | ||
return dataKey; | ||
} | ||
|
||
private byte[] Invert(byte[] data) | ||
{ | ||
byte[] dataKey = this.CreateKey(data); | ||
byte[] result = new byte[data.Length]; | ||
for (int i = 0; i < data.Length; i++) | ||
{ | ||
result[i] = (byte) (data[i] ^ dataKey[i]); | ||
} | ||
return result; | ||
} | ||
|
||
public byte[] Encode(byte[] data) | ||
{ | ||
return Invert(data); | ||
} | ||
|
||
public byte[] Decode(byte[] data) | ||
{ | ||
return Invert(data); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using UnityEngine; | ||
using Unicache.Plugin; | ||
using UnityEngine; | ||
|
||
namespace Unicache | ||
{ | ||
|