Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How can I generate a HmacSHA256 signature of a string

Tags:

php

hmac

Is there any way to create a HmacSHA256 signature of a string in php?

like image 560
tarnfeld Avatar asked Apr 25 '10 11:04

tarnfeld


2 Answers

Use hash_hmac:

$sig = hash_hmac('sha256', $string, $secret) 

Where $secret is your key.

like image 79
Sebastian Paaske Tørholm Avatar answered Oct 07 '22 05:10

Sebastian Paaske Tørholm


The hash_hmac() function could help, here :

Generate a keyed hash value using the HMAC method


For example, the following portion of code :

$hash = hash_hmac('sha256', 'hello, world!', 'mykey'); var_dump($hash); 

Gives the following output :

string '07a932dd17adc59b49561f33980ec5254688a41f133b8a26e76c611073ade89b' (length=64) 


And, to get the list of hashing algorithms that can be used, see hash_algos().

like image 29
Pascal MARTIN Avatar answered Oct 07 '22 05:10

Pascal MARTIN