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

Ethan Allen

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.

Ken Lee

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); 

?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make the same SHA256 encoded string in Javascript that I do in Python 3?

using this code how do you know if sha256 or sha512 would be used?

How can convert this code from golang to reactjs in crypto hmac sha256 hex

How to encrypt data with sha256 with hash_hmac in Rails like in this php function

How do I turn SwitchCompat on or off in code

How do I specify the key for computing a SHA256 hash?

How do I create SHA256 HMAC using Ironclad in Common Lisp?

How to replicate hash_hmac('sha256', $key, $secret_key) function in Swift 4?

How do i properly use the replace function within my code?

How so I convert this SHA256 + BASE64 from Swift to PHP?

How to convert from Hashbytes function's SHA2_256 encryption datatype to varchar(256) in SQL Server 2012

How do I convert an array based on keys in PHP?

How to fix encryption of sha256 hmac on google closure library?

Swift SHA256 hash doedn't match PHP SHA256 hash

How to do a basic sha256 encryption that matches the results on (website in description)

How Do I Convert a Picture into a Sha256 Hash Python

How do i convert android math function to swift with logb

How do I sha256 raw bits in python?

How can I convert this encryption function from Swift to Objective-C?

How do I get this code to loop based off the user's input?

How do I convert this to Swift?

Convert this php encryption function to python

Convert C# Encoding SHA256 code to Javascript

How do I convert this line of code to swift 2.0?

Convert .net aes256 encryption code into php

how do I convert this python code in python function?

How do I get Google Apps Script to do SHA-256 encryption?

How do I print a fingerprint (sha256 bytes) using colon notation?

How do I convert an integer to an enum based on its range in Swift?