Home > Blockchain >  How do I properly convert this SHA256 encryption function in PHP based off this Swift code?
How do I properly convert this SHA256 encryption function in PHP based off this Swift code?

Time:10-13

I have the following code in Swift (iOS) to do some basic data encryption:

import Foundation
import CommonCrypto

// ... 

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
}

I'm unfamiliar with encryption in PHP but need this simple function converted for a small project.

CodePudding user response:

For data encryption in PHP, you may use the hash function.

Just pass 'sha256' as the first parameter if you want to use sha256 hashing algorithm

Assuming that $data contains the data , then

<?php 

echo hash('sha256', $data); 

?>
  • Related