作者:企鹅田先生 | 来源:互联网 | 2023-05-17 13:33
Heresmyproblem.ImcurrentlytryingtoimplementafewCryptographystandardsinPHPforcompati
Here's my problem. I'm currently trying to implement a few Cryptography standards in PHP for compatibility reasons. The one I'm working on now is SHA256 and SHA512. They are both reasonably straight forward standards, and I'm not having an issue with that.
这是我的问题。出于兼容性原因,我目前正在尝试在PHP中实现一些加密标准。我现在正在研究的是SHA256和SHA512。它们都是相当直接的标准,我对此没有任何问题。
However, SHA512 requires bitwise operations on 64 bit integers. Since PHP can have only 32 bit integers (with the compile), that leaves me with a problem. How do I implement the bitwise functions necessary (modulo, shift, rotate, add, xor, and, or, etc) so that I both maintain compatibility and a reasonable level of performance...
但是,SHA512需要对64位整数进行逐位运算。由于PHP只能有32位整数(使用编译),这让我有一个问题。如何实现必要的按位函数(模数,移位,旋转,添加,xor和/或等),以便我保持兼容性和合理的性能水平......
I know for some of them implementing the functions is trivially done with 2 32 bit ints. However, how will that work for shift
and rotate
?
我知道其中一些实现这些功能的工作只需要2个32位整数。但是,如何进行转换和旋转?
What I've thought of doing is storing the strings in binary form (as a string of 01010
). That way all bitwise operations will be completely architecture independent. But this will likely cause a massive performance drop since they are used incredibly frequently in the standards (and in other parts of the library).
我想到的是以二进制形式存储字符串(作为字符串01010)。这样,所有按位操作都将完全独立于架构。但这可能会导致性能大幅下降,因为它们在标准(以及库的其他部分)中经常使用。
So my question is thus: How can I easily allow for at least 64 bit string operations in a 32 bit compile of PHP while still maintaining a reasonable level for performance for every step...?
因此我的问题是:如何在PHP的32位编译中轻松地允许至少64位字符串操作,同时仍然保持每个步骤的性能合理水平......?
Oh, and my aim is portability here, so no extensions. Other libraries I'll consider, but they must be portable...
哦,我的目标是可移植性,所以没有扩展。我会考虑的其他图书馆,但它们必须是便携式的......
3 个解决方案