Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何转换这个SHA256+;BASE64从Swift到PHP?_Php_Swift_Base64_Sha - Fatal编程技术网

我如何转换这个SHA256+;BASE64从Swift到PHP?

我如何转换这个SHA256+;BASE64从Swift到PHP?,php,swift,base64,sha,Php,Swift,Base64,Sha,我收到了这段Swift代码,可以尝试在PHP中工作: finalStr = Encryption.sha256(inputStr) ... class Encryption { static func sha256(_ data: Data) -> Data? { guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }

我收到了这段Swift代码,可以尝试在PHP中工作:

finalStr = Encryption.sha256(inputStr)

...

    class Encryption {
        static func sha256(_ data: Data) -> Data? {
            guard let res = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH)) else { return nil }
            CC_SHA256((data as NSData).bytes, CC_LONG(data.count), res.mutableBytes.assumingMemoryBound(to: UInt8.self))
            return res as Data
        }

        static func sha256(_ str: String) -> String? {
            guard
                let data = str.data(using: String.Encoding.utf8),
                let shaData = Encryption.sha256(data)
                else { return nil }
            let rc = shaData.base64EncodedString(options: [])
            return rc
        }
    }
我在PHP中执行以下操作,但最终结果不匹配:

$hashedStr = hash('sha256', $inputStr);
$finalStr = base64_encode($hashedStr);
echo $finalStr;

PHP方面缺少什么?

对于PHP中的
hash
方法,应该将原始输出设置为true。请注意中的第三个方法参数


这样,来自PHP的base64_编码的原始值应该等于从
base64EncodedString

获得的值,谢谢!我误解了第三个论点的目的。。。但这基本上就是str.data所做的。不知道为什么我被否决了?
$hashedStr = hash('sha256', $inputStr, true);
$finalStr = base64_encode($hashedStr);
echo $finalStr;