VB.NET 和 sizeof

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26407294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:21:56  来源:igfitidea点击:

VB.NET and sizeof

c#.netvb.netsizeof

提问by Kevin Brydon

I'm converting some code from C# to VB.NET. I have the following line in C#

我正在将一些代码从 C# 转换为 VB.NET。我在 C# 中有以下行

var bytes = new byte[password.Length * sizeof(char)];

Looking on MSDNit appears that VB.NET does not seem to have the sizeofoperator. I understand there is a Marshal.SizeOfbut further MSDN documentationstates that the value returned can be different to that of sizeof.

MSDN 上看,VB.NET 似乎没有sizeof运营商。我知道有一个Marshal.SizeOf但进一步的MSDN 文档指出返回的值可能与sizeof.

Can anybody help? Is there an equivalent statement in VB.NET?

有人可以帮忙吗?VB.NET 中是否有等效的语句?

Additional Information

附加信息

My aim is to convert a password into an array of bytes which I can then hash and then either store in a database or compare to a previously stored hash. But I don't necessarily want an answer relating to my particular situation.

我的目标是将密码转换为字节数组,然后我可以对其进行散列,然后将其存储在数据库中或与之前存储的散列进行比较。但我不一定想要与我的特定情况有关的答案。

Dim bytes(password.Length * xxx) As Byte
System.Buffer.BlockCopy(password.ToCharArray(), 0, bytes, 0, bytes.Length)
Dim sha512 = System.Security.Cryptography.SHA512.Create()
Dim hash = sha512.ComputeHash(bytes)

' compare hash or stroe in database

回答by Dave Doknjas

The 'Len' operator in VB will do this (but it works on instances, so you need to adjust accordingly):

VB 中的“Len”运算符将执行此操作(但它适用于实例,因此您需要相应地进行调整):

Dim bytes = New Byte((someString.Length * Len(New Char)) - 1){}

回答by stakx - no longer contributing

VB.NET's Charmaps to .NET's System.Char, which is defined in ECMA 335to be a 16-bit Unicode character. Meaning, Charhas a fixed size (no matter on which platform you compile or run your code), you don't actually need sizeof.

VB.NET'sChar映射到 .NET's System.Char,它在ECMA 335 中定义为 16 位 Unicode 字符。意思是,Char具有固定大小(无论您在哪个平台上编译或运行代码),您实际上都不需要sizeof.

Therefore, just multiply by 2.

因此,只需乘以2

回答by Joel Coehoorn

I'm hashing a password and was following stackoverflow.com/a/10380166/1113475

我正在对密码进行哈希处理并关注 stackoverflow.com/a/10380166/1113475

The top answer there is wrong, in spite of high vote count. The code for that answer still uses an encoding (Unicode), because that's how all strings are encoded internally in .NET. Even if it didn't, the encoding still matters, because you need to be able to decrypt the string on a different system than the one that encrypted it and obtain meaningful results. Even with the same system doing the encrypting/decrypting, something as simple as a Windows Update patch to the .NET Framework could break this. Pick an encoding (like Unicode or UTF-8), and just call its GetBytes()method:

尽管投票数很高,但最重要的答案是错误的。该答案的代码仍然使用编码 (Unicode),因为这就是 .NET 中所有字符串的内部编码方式。即使没有,编码仍然很重要,因为您需要能够在与加密字符串不同的系统上解密字符串并获得有意义的结果。即使使用相同的系统进行加密/解密,像 .NET Framework 的 Windows 更新补丁这样简单的事情也可能会破坏这一点。选择一种编码(如 Unicode 或 UTF-8),然后调用它的GetBytes()方法:

Dim bytes = Encoding.Unicode.GetBytes(password)