using System; using System.Collections.Generic; namespace NFC.Helper { public static class ByteOperation { /// /// Generate Byte Array filled with 0 /// /// Size of Array public static byte[] GenerateEmptyArray(uint size) { byte[] key = new byte[size]; for (int i = 0; i < size; i++) { key[i] = 0; } return key; } /// /// Get Range of Array Elements /// /// Array /// Offset in Byte /// Lenght to read in Byte /// new Array with Range of Array Elements public static byte[] GetSubArray(byte[] array, long offset, long length) { byte[] subarray = new byte[length]; for (long i = offset; i < offset + length; i++) { subarray[i - offset] = array[i]; } return subarray; } /// /// Return a copy of the last Block of data /// /// Data compatible to blocksize /// in byte public static byte[] ExtractLastBlock(byte[] data, uint blocksize) { if (data == null) { throw new ArgumentNullException("Data cannot be null."); } if (data.Length % blocksize != 0) { throw new ArgumentException(string.Format("Data is not compatible with blocksize(data(length):{0}, blocksize:{1}.", data.Length, blocksize)); } byte[] lastblock = new byte[blocksize]; for (int i = 0; i < blocksize; i++) { lastblock[i] = data[data.Length - blocksize + i]; } return lastblock; } /// /// Expand Array to Block Size, fill with 0x00 /// /// public static byte[] ExpandToBlockSize(byte[] data, uint bocksize) { if (data == null) { throw new ArgumentNullException("Data cannot be null."); } int diff = data.Length % (int)bocksize; if (diff == 0) { return data; } byte[] expand = new byte[data.Length + bocksize - diff]; data.CopyTo(expand, 0); for (int i = expand.Length - 1; i > data.Length - 1; i--) { expand[i] = 0x00; } return expand; } /// /// Rotates Array to the left /// /// Data /// Copy of data public static byte[] RotateLeft(byte[] data) { if (data == null) { throw new ArgumentNullException("Data cannot be null."); } byte[] rotate = new byte[data.Length]; data.CopyTo(rotate, 0); byte tmp = rotate[0]; for (var i = 0; i < rotate.Length - 1; i++) { rotate[i] = rotate[i + 1]; } rotate[rotate.Length - 1] = tmp; return rotate; } /// /// Rotates Array to the right /// /// Data /// Copy of data public static byte[] RotateRight(byte[] data) { if (data == null) { throw new ArgumentNullException("Data cannot be null."); } byte[] rotate = new byte[data.Length]; data.CopyTo(rotate, 0); byte tmp = rotate[rotate.Length - 1]; for (var i = rotate.Length - 1; i > 0; i--) { rotate[i] = rotate[i - 1]; } rotate[0] = tmp; return rotate; } /// /// Concatenates Arrays /// /// List of Byte Array public static byte[] Concatenate(params byte[][] data) { if (data == null) { throw new ArgumentNullException("Data cannot be null."); } List cat = new List(); foreach (byte[] d in data) { cat.AddRange(d); } return cat.ToArray(); } /// /// Boolean Operation XOR on all Bytes /// /// Array A /// Array B /// Copy of Data public static byte[] XOR(byte[] a, byte[] b) { if (a == null) { throw new ArgumentNullException("Array A cannot be null."); } if (b == null) { throw new ArgumentNullException("Array B cannot be null."); } if (a.Length != b.Length) { throw new ArgumentException(string.Format("Arrays are not same Length(Length A:{0}, Lenght B:{1})", a.Length, b.Length)); } byte[] c = new byte[a.Length]; for (int i = 0; i < a.Length; i++) { c[i] = (byte)(a[i] ^ b[i]); } return c; } } }