相当于PHP Unpack和SHA1的ColdFusion [英] ColdFusion Equivalent to PHP Unpack and SHA1

查看:39
本文介绍了相当于PHP Unpack和SHA1的ColdFusion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.在PHP中,我很难在ColdFusion中复制两个函数.

Here is my problem. In PHP there are two functions that I am having a hard time replicating in ColdFusion.

1)PHP的解压缩功能,可让您以给定的方式格式化输出.格式选项之一是"H",它代表十六进制字符串,高半字节在前"-我该如何在ColdFusion中复制它?

1) PHP's unpack function which let's you format the output in a given way. One of the format options is 'H' which stands for "Hex string, high nibble first" - How could I replicate this in ColdFusion?

这是我到目前为止所拥有的...

This is what I have so far...

<cfscript>
function stringToBinary( String stringValue ){
    var base64Value = toBase64( stringValue );
    var binaryValue = toBinary( base64Value );
    return( binaryValue );
}

function stringToHex( String stringValue ){
    var binaryValue = stringToBinary( stringValue );
    var hexValue = binaryEncode( binaryValue, "hex" );
    return( lcase( hexValue ) );
}   
</cfscript>

因此,我无法确定在上述实现中指定高半字节(同样,我也没有写这本冷融合词,这是由Ben Nadel提供的).

So I can't figure out to specify high nibble in the implementation above (also, I did not write this coldfusion this is courtesy of Ben Nadel).

第二,在PHP中,有一个用于散列的SHA1函数...该函数有一个参数,它是一个布尔参数,用于如果可选的raw_output设置为TRUE,那么将返回sha1摘要格式为原始二进制格式 ,长度为20 ,否则返回的值为40个字符的十六进制数字."因此,我的Coldfusion模仿它只是使用Hash()函数并将其指定为使用"SHA-1",然后将其转换为二进制...但是我如何以与PHP完全相同的方式正确地将其限制为"20"是吗?

Secondly, in PHP there is a SHA1 function for hashing... there is an argument to that function which is a boolean argument for "If the optional raw_output is set to TRUE, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number." So my Coldfusion to mimic this is simply using the Hash() function and specifying it to use "SHA-1" and then converting that to binary... but how would I correctly limit it "to 20" in the same exact way PHP does?

我知道这涉及到我在这两个问题上已经困扰了很长时间……非常感谢您的帮助.

I know this is involved by I have been stumped for quite some time now on these two issues... Any help is very much appreciated.

推荐答案

使用Hash()函数和指定它使用"SHA-1",然后将其转换为二进制...但是我如何正确地将其限制为"20"

using the Hash() function and specifying it to use "SHA-1" and then converting that to binary... but how would I correctly limit it "to 20"

您无需执行任何操作. SHA1产生160位.二进制结果将始终为20个字节,或40个字符编码为十六进制(每字节两个字符)

You do not need to do anything. SHA1 produces 160 bits. The binary result will always be 20 bytes, or 40 characters encoded as hexadecimal (two characters per byte).

  hashString = hash("Something wicked this way comes", "sha1");
  hashBinary = binaryDecode( hashString, "hex");
  writeOutput(" String / len("& len(hashString) &")");
  writeOutput(" Binary / arrayLen("& arrayLen(hashBinary) &")");

"H"代表十六进制字符串,高位优先"找出在上面的实现中指定高位蚕食

'H' which stands for "Hex string, high nibble first" figure out to specify high nibble in the implementation above

您是否有特定原因认为您必须做一些特殊/额外的事情?根据我的阅读,典型的函数用于在PHP中生成十六进制,即 bin2hex ,默认设置为高半字节,CF也是如此.除了CF将结果转换为大写字母外,结果是相同的.您能否提供一个具体示例,说明PHP和CF结果的不同之处?

Is there a specific reason you believe you must do something special/extra? From what I have read, the typical function used to generate hex in PHP, ie bin2hex, defaults to high nibble first, as does CF. Aside from the fact that CF converts the results to upper case, the results are identical. Can you provide a specific example of where the PHP and CF results differ?

PHP:

echo bin2hex("Green Onions");
// result:  477265656e204f6e696f6e73

CF

writeDump( binaryEncode(charsetDecode("Green Onions", "utf-8"), "hex") );
// result: 477265656E204F6E696F6E73 

这篇关于相当于PHP Unpack和SHA1的ColdFusion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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