//Benton Stark 10-21-2013 /// <summary> /// Compute a hash value across one or more byte arrays. /// </summary> /// <param name="algo">Hashing algorithm to use.</param> /// <param name="list">Byte array to compute hash across.</param> /// <returns>Byte array containing hash value.</returns> public static byte[] Hash(HashAlgorithm algo, params byte[][] list) { if (algo == null) { throw new ArgumentNullException("algo"); } if (list == null) { throw new ArgumentNullException("list"); } int size = 0; foreach (byte[] item in list) { if (item == null) { throw new ArgumentNullException("list contains null array"); } size += item.Length; } ArrayBuilder b = new ArrayBuilder(size); foreach (byte[] item in list) { b.Append(item); } byte[] hash = algo.ComputeHash(b.GetBytes()); b.Clear(); return(hash); }
//Benton Stark 10-21-2013 /// <summary> /// Combine 2 or more byte arrays into a single, contegious byte array. /// </summary> /// <param name="list">Byte array parameter list.</param> /// <returns>Single byte array.</returns> public static byte[] Combine(params byte[][] list) { if (list == null) { throw new ArgumentNullException("list"); } int size = 0; foreach (byte[] item in list) { if (item == null) { throw new ArgumentNullException("list contains null array"); } size += item.Length; } ArrayBuilder b = new ArrayBuilder(size); foreach (byte[] item in list) { b.Append(item); } byte[] rtn = b.GetBytes(); b.Clear(); return(rtn); }
//Benton Stark 10-21-2013 /// <summary> /// Zero pads an array of bytes to specific size. /// </summary> /// <param name="data">Data to zero pad.</param> /// <param name="size">Size of the return array.</param> /// <returns>Array of bytes zero padded.</returns> public static byte[] ZeroPad(byte[] data, int size) { if (data == null) { throw new ArgumentNullException("data"); } if (data.Length > size) { throw new ArgumentOutOfRangeException("size"); } ArrayBuilder b = new ArrayBuilder(size); b.Append(data); byte[] pad = b.GetBytes(); b.Clear(); // clear the private key from the array builder memory return(pad); // return pointer to the byte array }
//Benton Stark 07-25-2014 /// <summary> /// Pads an array of bytes to specific size using a supplied padding value. /// </summary> /// <param name="data">Data to pad.</param> /// <param name="size">Size of the return array.</param> /// <param name="padValue">Padding value to use.</param> /// <returns>Array of bytes padded.</returns> public static byte[] PadArray(byte[] data, int size, byte padValue) { if (data == null) { throw new ArgumentNullException("data"); } if (data.Length > size) { throw new ArgumentOutOfRangeException("size"); } ArrayBuilder b = new ArrayBuilder(size); b.Append(data); b.Append(ArrayUtils.CreateArray(size - data.Length, padValue)); byte[] pad = b.GetBytes(); // get a copy of the padded bytes b.Clear(); // clear the private key from the array builder memory return(pad); // return pointer to the byte array }
//Benton Stark 10-21-2013 /// <summary> /// Compute a hash value across one or more byte arrays. /// </summary> /// <param name="algo">Hashing algorithm to use.</param> /// <param name="list">Byte array to compute hash across.</param> /// <returns>Byte array containing hash value.</returns> public static byte[] Hash(HashAlgorithm algo, params byte[][] list) { if (algo == null) throw new ArgumentNullException("algo"); if (list == null) throw new ArgumentNullException("list"); int size = 0; foreach (byte[] item in list) { if (item == null) throw new ArgumentNullException("list contains null array"); size += item.Length; } ArrayBuilder b = new ArrayBuilder(size); foreach (byte[] item in list) { b.Append(item); } byte[] hash = algo.ComputeHash(b.GetBytes()); b.Clear(); return hash; }
//Benton Stark 07-25-2014 /// <summary> /// Pads an array of bytes to specific size using a supplied padding value. /// </summary> /// <param name="data">Data to pad.</param> /// <param name="size">Size of the return array.</param> /// <param name="padValue">Padding value to use.</param> /// <returns>Array of bytes padded.</returns> public static byte[] PadArray(byte[] data, int size, byte padValue) { if (data == null) throw new ArgumentNullException("data"); if (data.Length > size) throw new ArgumentOutOfRangeException("size"); ArrayBuilder b = new ArrayBuilder(size); b.Append(data); b.Append(ArrayUtils.CreateArray(size - data.Length, padValue)); byte[] pad = b.GetBytes(); // get a copy of the padded bytes b.Clear(); // clear the private key from the array builder memory return pad; // return pointer to the byte array }
//Benton Stark 10-21-2013 /// <summary> /// Zero pads an array of bytes to specific size. /// </summary> /// <param name="data">Data to zero pad.</param> /// <param name="size">Size of the return array.</param> /// <returns>Array of bytes zero padded.</returns> public static byte[] ZeroPad(byte[] data, int size) { if (data == null) throw new ArgumentNullException("data"); if (data.Length > size) throw new ArgumentOutOfRangeException("size"); ArrayBuilder b = new ArrayBuilder(size); b.Append(data); byte[] pad = b.GetBytes(); b.Clear(); // clear the private key from the array builder memory return pad; // return pointer to the byte array }
//Benton Stark 10-21-2013 /// <summary> /// Combine 2 or more byte arrays into a single, contegious byte array. /// </summary> /// <param name="list">Byte array parameter list.</param> /// <returns>Single byte array.</returns> public static byte[] Combine(params byte[][] list) { if (list == null) throw new ArgumentNullException("list"); int size = 0; foreach (byte[] item in list) { if (item == null) throw new ArgumentNullException("list contains null array"); size += item.Length; } ArrayBuilder b = new ArrayBuilder(size); foreach (byte[] item in list) { b.Append(item); } byte[] rtn = b.GetBytes(); b.Clear(); return rtn; }