singrdk/base/Applications/Security/CryptoBvt/Util.cs

120 lines
3.5 KiB
C#
Raw Permalink Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
using System;
using System.Text;
class Util
{
public const string HexDigits = "0123456789abcdef";
public static string! ByteArrayToStringHex(byte[]! buffer, int index, int length)
{
StringBuilder sb = new StringBuilder(length * 2);
2008-11-17 18:29:00 -05:00
for (int i = 0; i < length; i++) {
2008-03-05 09:52:00 -05:00
byte b = buffer[index + i];
sb.Append(HexDigits[b >> 4]);
sb.Append(HexDigits[b & 0xf]);
}
return sb.ToString();
}
public static string! ByteArrayToStringHex(byte[]! buffer)
{
return ByteArrayToStringHex(buffer, 0, buffer.Length);
}
static byte CharToHex(char c)
{
if (c >= '0' && c <= '9')
return (byte)(c - '0');
if (c >= 'a' && c <= 'f')
return (byte)(c - 'a' + 10);
if (c >= 'A' && c <= 'F')
return (byte)(c - 'A' + 10);
throw new ArgumentException("Invalid hex char");
}
public static byte[]! HexStringToByteArray(string! str)
{
if ((str.Length % 2) != 0)
throw new Exception("Input string cannot be odd in length.");
byte[] result = new byte[str.Length / 2];
2008-11-17 18:29:00 -05:00
for (int i = 0; i < result.Length; i++) {
2008-03-05 09:52:00 -05:00
byte high = CharToHex(str[i * 2]);
byte low = CharToHex(str[i * 2 + 1]);
result[i] = (byte)((high << 4) | low);
}
return result;
}
public static int CompareArraySpans(byte[]! array1, int offset1, byte[]! array2, int offset2, int length)
{
2008-11-17 18:29:00 -05:00
for (int i = 0; i < length; i++) {
2008-03-05 09:52:00 -05:00
byte element1 = array1[i];
byte element2 = array2[i];
if (element1 < element2)
return -1;
if (element1 > element2)
return 1;
}
return 0;
}
public static void DumpBuffer(byte[]! buffer)
{
DumpBuffer(buffer, 0, buffer.Length);
}
public static void DumpBuffer(byte[]! buffer, int index, int length)
{
StringBuilder line = new StringBuilder();
2008-11-17 18:29:00 -05:00
for (int i = 0; i < length; i += 0x10) {
2008-03-05 09:52:00 -05:00
line.Length = 0;
line.AppendFormat("{0:x04}: ", i);
2008-11-17 18:29:00 -05:00
for (int j = 0; j < 0x10; j++) {
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
if (i + j < length) {
2008-03-05 09:52:00 -05:00
line.Append(" ");
byte b = buffer[index + i + j];
line.Append((Char)HexDigits[b >> 4]);
line.Append((Char)HexDigits[b & 0xf]);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
line.Append(" ");
}
}
line.Append(" : ");
2008-11-17 18:29:00 -05:00
for (int j = 0; j < 0x10; j++) {
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
if (i + j < length) {
2008-03-05 09:52:00 -05:00
byte b = buffer[index + i + j];
2008-11-17 18:29:00 -05:00
if (b >= 32 && b <= 127) {
2008-03-05 09:52:00 -05:00
line.Append((Char)b);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
line.Append(".");
}
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
break;
}
}
Console.WriteLine(line.ToString());
}
}
}