add another form of bytesToHexString(byte[] bytes, bool lowercase)

This commit is contained in:
UbitUmarov
2020-12-29 14:41:41 +00:00
parent 0e8ea0486d
commit 71065f19ad
2 changed files with 59 additions and 10 deletions

View File

@@ -38,6 +38,7 @@ using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
@@ -1136,6 +1137,61 @@ namespace OpenSim.Framework
/// <param name="data"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char LowNibbleToHexByteCharLowcaps(byte b)
{
b &= 0x0f;
return (char)(b > 9 ? b + 0x57 : b + '0');
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char HighNibbleToHexByteCharLowcaps(byte b)
{
b >>= 4;
return (char)(b > 9 ? b + 0x57 : b + '0');
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char LowNibbleToHexByteCharHighcaps(byte b)
{
b &= 0x0f;
return (char)(b > 9 ? b + 0x57 : b + '0');
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char HighNibbleToHexByteCharHighcaps(byte b)
{
b >>= 4;
return (char)(b > 9 ? b + 0x57 : b + '0');
}
public static string bytesToHexString(byte[] bytes, bool lowerCaps)
{
if(bytes == null || bytes.Length == 0)
return string.Empty;
char[] chars = new char[2* bytes.Length];
if(lowerCaps)
{
for (int i = 0, j = 0; i < bytes.Length; ++i)
{
byte b = bytes[i];
chars[j++] = HighNibbleToHexByteCharLowcaps(b);
chars[j++] = LowNibbleToHexByteCharLowcaps(b);
}
}
else
{
for (int i = 0, j = 0; i < bytes.Length; ++i)
{
byte b = bytes[i];
chars[j++] = HighNibbleToHexByteCharHighcaps(b);
chars[j++] = LowNibbleToHexByteCharHighcaps(b);
}
}
return new string(chars);
}
public static string SHA1Hash(string data, Encoding enc)
{
return SHA1Hash(enc.GetBytes(data));
@@ -1154,7 +1210,7 @@ namespace OpenSim.Framework
public static string SHA1Hash(byte[] data)
{
byte[] hash = ComputeSHA1Hash(data);
return BitConverter.ToString(hash).Replace("-", String.Empty);
return bytesToHexString(hash, false);
}
private static byte[] ComputeSHA1Hash(byte[] src)

View File

@@ -2612,16 +2612,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
// Create a SHA256
using (SHA256 sha256Hash = SHA256.Create())
{
// ComputeHash - returns byte array
// ComputeHash - returns byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
return Util.bytesToHexString(bytes, true);
}
}