Пример #1
0
 //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;
 }
Пример #2
0
 //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
 }
Пример #3
0
 //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;
 }
Пример #4
0
 //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
 }
Пример #5
0
 /// <summary>
 /// Converts a 24-bit integer byte array to a 32-bit integer.
 /// </summary>
 /// <param name="int24">24-bit integer value.</param>
 /// <returns>Unsigned 32-bit integer value.</returns>
 public static Int32 GetInt32(byte[] int24)
 {
     if (int24 == null)
         throw new ArgumentNullException("int24");
     
     if (int24.Length != 3)
     {
         throw new ArgumentOutOfRangeException("int24", "byte size must be exactly three");
     }
     ArrayBuilder buffer = new ArrayBuilder(4);
     buffer.Append(int24);
     return BitConverter.ToInt32(buffer.GetBytes(), 0);
 }