首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将具有Byte序列的字符串转换为Byte的最佳方法

将具有Byte序列的字符串转换为Byte的最佳方法
EN

Code Review用户
提问于 2022-03-16 23:04:14
回答 1查看 1.3K关注 0票数 -1

我们有一个带有byte array (十六进制)序列的D1(十六进制)序列,比如:"0x65,0x31,0xb6,0x9e,0xaf,0xd2,0x39,0xc9,0xad,0x07,0x78,0x99,0x73,0x52,0x91,0xf5,0x93,0x1a,0x49,0xc6",我们需要再次将这个序列恢复到byte array

我们采用以下方法:

代码语言:javascript
复制
string byteSequence = "0x65,0x31,0xb6,0x9e,0xaf,0xd2,0x39,0xc9,0xad,0x07,0x78,0x99,0x73,0x52,0x91,0xf5,0x93,0x1a,0x49,0xc6";

byte[] myBytes = stringByteSequence.Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();

这个哈希序列是由这个样本生成的:

代码语言:javascript
复制
string password = "";

using (var cryptoProvider = System.Security.Cryptography.SHA1.Create())
{
    byte[] passwordHash = cryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(password));
    string result = "new byte[] { " + 
        String.Join(",", passwordHash.Select(x => "0x" + x.ToString("x2")).ToArray())
         + " } ";
  
    //...
    // result = "new byte[] { 0x65,0x31,0xb6,0x9e,0xaf,0xd2,0x39,0xc9,0xad,0x07,0x78,0x99,0x73,0x52,0x91,0xf5,0x93,0x1a,0x49,0xc6 }"
    //...
}

是否有一种“更清洁”的方法来进行这一转换?

Illustrative代码,如:

代码语言:javascript
复制
byte[] myBytes = byteSequence.Which.Method.I.Can.Use.To.Convert.It.To.Byte.Array.Again?();

原始样本(以上参考链接):

代码语言:javascript
复制
new BasicAuthAuthorizationUser
{
      Login = "Administrator-2",
      // Password as SHA1 hash
      Password = new byte[]{0xa9,
                 0x4a, 0x8f, 0xe5, 0xcc, 0xb1, 0x9b,
                 0xa6, 0x1c, 0x4c, 0x08, 0x73, 0xd3,
                 0x91, 0xe9, 0x87, 0x98, 0x2f, 0xbb,
                 0xd3}
}

我们的主要想法是:

代码语言:javascript
复制
new BasicAuthAuthorizationUser
{
      Login = "Administrator-2",
      // Password as SHA1 hash
      Password = configuration["MY_ENV_VAR_NAME"].Split(',').Select(s => Convert.ToByte(s, 16)).ToArray();

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2022-03-18 02:16:22

不确定为什么要这样存储十六进制散列,如果您只使用普通的十六进制,它将更加可读性更强,并且可以防止添加不必要的工作。

对于当前的工作,可以使用扩展方法将十六进制字符串转换为字节,反之亦然。

代码语言:javascript
复制
public static class Extensions
{
    public static string ToHexadecimalSeparatedString(this byte[] bytes) => bytes != null ? string.Join(",", bytes.Select(x => $"0x{x:X2}")) : null;
    
    public static byte[] FromHexadecimalSeparatedString(this string hexadecimal)
    {
        if (string.IsNullOrWhiteSpace(hexadecimal)) return null;
        return hexadecimal.Split(',').Select(x=> Convert.ToByte(x, 16)).ToArray();
    }
    
}

用法:

代码语言:javascript
复制
string byteSequence = "0x65,0x31,0xb6,0x9e,0xaf,0xd2,0x39,0xc9,0xad,0x07,0x78,0x99,0x73,0x52,0x91,0xf5,0x93,0x1a,0x49,0xc6";

byte[] bytes = byteSequence.FromHexadecimalSeparatedString();

string hexadecimal = bytes.ToHexadecimalSeparatedString();

所以在你的样本中应该是:

代码语言:javascript
复制
new BasicAuthAuthorizationUser
{
      Login = "Administrator-2",
      // Password as SHA1 hash
      Password = configuration["MY_ENV_VAR_NAME"].FromHexadecimalSeparatedString()

}

尽管我仍然不相信十六进制的存储方式,但是如果您打算将其还原为普通十六进制(没有分隔符,也没有十六进制前缀),那么您可能对使用以下扩展感兴趣:

代码语言:javascript
复制
public static class Extensions
{
    public static string ToHexadecimal(this byte[] bytes) => bytes != null ? string.Concat(bytes.Select(x => $"{x:X2}")) : null;
    
    public static byte[] FromHexadecimal(this string hexadecimal, string separater = null)
    {
        if(string.IsNullOrWhiteSpace(hexadecimal)) return null;

        if(!string.IsNullOrWhiteSpace(separater))
        {
            hexadecimal = hexadecimal.Replace(separater, string.Empty);
        }

        var temp = hexadecimal.Replace("0x", string.Empty);

        byte[] bytes = new byte[temp.Length / 2];

        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] = Convert.ToByte(temp.Substring(i * 2, 2), 16);
        }
        
        return bytes;
    }
    
}

下面的部分只是为了向后兼容性,以支持已经用分隔符存储的散列。如果所有存储的散列都是普通的(没有分隔符,没有十六进制前缀),那么您可以删除该部分。

代码语言:javascript
复制
if(!string.IsNullOrWhiteSpace(separater))
{
    hexadecimal = hexadecimal.Replace(separater, string.Empty);
}

var temp = hexadecimal.Replace("0x", string.Empty);
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/274990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档