是否有比BitConverter.IsLittleEndian更好的方法来检测.NET中的字节序? [英] Is there a better way to detect endianness in .NET than BitConverter.IsLittleEndian?

查看:99
本文介绍了是否有比BitConverter.IsLittleEndian更好的方法来检测.NET中的字节序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果.NET框架只提供了BitConverter类的函数/方法,则该类仅以适当的请求字节序显式返回了字节数组。

It would be nice if the .NET framework just gave functions/methods from the BitConverter class that just explicitly returned an array of bytes in the proper requested endianness.

我已经在其他代码中完成了一些类似的功能,但是有没有更短的更直接的方法? (效率是关键,因为此概念在PBKDF2,Skein,HMAC,BLAKE2,AES等各种加密和密码派生上下文中使用了TON)

I've done some functions like this in other code, but is there a shorter more direct way? (efficiency is key since this concept is used a TON in various crypto and password derivation contexts, including PBKDF2, Skein, HMAC, BLAKE2, AES and others)

// convert an unsigned int into an array of bytes BIG ENDIEN
// per the spec section 5.2 step 3 for PBKDF2 RFC2898
static internal byte[] IntToBytes(uint i)
{
    byte[] bytes = BitConverter.GetBytes(i);
    if (!BitConverter.IsLittleEndian)
    {
        return bytes;
    }
    else
    {
        Array.Reverse(bytes);
        return bytes;
    }
}

我还看到其他人对此问题感到困惑,并且我还没有一个好的答案:( 如何处理字节序

I also see that others struggle with this question, and I haven't seen a good answer yet :( How to deal with 'Endianness'

推荐答案

我在整数和 byte [] 之间转换的方式是

使用具有固定字节序的位移位,您无需担心此类代码的主机字节序。当您非常在意性能时,应该避免每次分配新数组。

The way I convert between integers and byte[] is by using bitshifts with fixed endianness. You don't need to worry about host endianness with such code. When you care that much about performance, you should avoid allocating a new array each time.

在我的密码库中,我使用:

In my crypto library I use:

public static UInt32 LoadLittleEndian32(byte[] buf, int offset)
{
    return
        (UInt32)(buf[offset + 0])
    | (((UInt32)(buf[offset + 1])) << 8)
    | (((UInt32)(buf[offset + 2])) << 16)
    | (((UInt32)(buf[offset + 3])) << 24);
}

public static void StoreLittleEndian32(byte[] buf, int offset, UInt32 value)
{
    buf[offset + 0] = (byte)value;
    buf[offset + 1] = (byte)(value >> 8);
    buf[offset + 2] = (byte)(value >> 16);
    buf[offset + 3] = (byte)(value >> 24);
}

使用大字节序,您只需要更改移位量或偏移量即可:

With big endian you just need to change the shift amounts or the offsets:

public static void StoreBigEndian32(byte[] buf, int offset, UInt32 value)
{
    buf[offset + 3] = (byte)value;
    buf[offset + 2] = (byte)(value >> 8);
    buf[offset + 1] = (byte)(value >> 16);
    buf[offset + 0] = (byte)(value >> 24);
}

如果目标是.net 4.5,则标记这些方法会很有用使用 [MethodImpl(MethodImplOptions.AggressiveInlining)]

If you're targetting .net 4.5 it can be useful to mark these methods with [MethodImpl(MethodImplOptions.AggressiveInlining)].

加密的另一个性能提示是尽可能避免使用数组。在函数的开头加载数组中的数据,然后使用局部变量运行所有内容,并且仅在最后复制回数组。

Another performance tip for crypto is avoiding arrays as much as possible. Load the data from the array at the beginning of the function, then run everything using local variables and only in the very end you copy back to the array.

这篇关于是否有比BitConverter.IsLittleEndian更好的方法来检测.NET中的字节序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆