How to sha256 a value in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

How to sha256 a value in PHP

Post by raghavan20 »

I have got a variable in PHP which has to be sha256'd. How it can be done?
Normally, in case of member registration we can do it when we get the values of the form in javascript. but how can we deal with this situation???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

explain more please.

Usage of the class is detailed in my posts about it. There's a "test" script that illustrates using it.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

alright feyd, I have a reset password option in the admin page.
I want to reset the password to a user's lastname when the user requests for password reset.
so I extract the last name of the user from the db and want to sha256 it and store it again in the db.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$hashed_string = SHA256::hash($string_to_hash);
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Is there a class been written to sha256 a value in PHP??? I dont know about this. I thought that function is just available in Javascript alone.
Its good really!!!
Is it possible to see the class definition???
It appears like I have to include the class? It does not recognize the existance of such a class, sha256. How do I do that???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

raghavan20 wrote:Is there a class been written to sha256 a value in PHP???
Yes, there is a class. I wrote it. Roja has tested it on many platforms, and I know I've seen several sites around the net linking to it. :)
raghavan20 wrote:I thought that function is just available in Javascript alone.
It basically was until I wrote mine :) There are various versions available through extensions to php, but none are always installed and loaded.
raghavan20 wrote:Is it possible to see the class definition???
Code Snippets: Security: SHA256 Hashing Algorithm Updated v1.1.0
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Its really good job
Thanks for supporting open source with your contributions.

A suggestion, you can give all those three php files in a zip file. It was a nightmare for me to copy them which are under the PHP tags. I had to click on QUOTE link to see the code in textarea and I copied them as new php files.

I downloaded those three files and included test_sha256.php in my admin page. but it displays a few messages. do I have to track the code to remove all of them? It also stops the code in my admin page from executing. how do I counter this???

Code: Select all

unning testSHA256::test1()

Testing 'abc'
array(1)::

string(64)::
61626380 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000018

end array;

array(8)::

string(4)::
6A09E667 
string(4)::
BB67AE85 
string(4)::
3C6EF372 
string(4)::
A54FF53A 
string(4)::
510E527F 
string(4)::
9B05688C 
string(4)::
1F83D9AB 
string(4)::
5BE0CD19 
end array;

processing took 0.00023100000000001 seconds.


Testing 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
array(2)::

string(64)::
61626364 62636465 63646566 64656667
65666768 66676869 6768696A 68696A6B
696A6B6C 6A6B6C6D 6B6C6D6E 6C6D6E6F
6D6E6F70 6E6F7071 80000000 00000000

string(64)::
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000
00000000 00000000 00000000 000001C0

end array;

array(8)::

string(4)::
6A09E667 
string(4)::
BB67AE85 
string(4)::
3C6EF372 
string(4)::
A54FF53A 
string(4)::
510E527F 
string(4)::
9B05688C 
string(4)::
1F83D9AB 
string(4)::
5BE0CD19 
end array;

processing took 0.000143 seconds.



running testSHA256::test2()

Testing 'abc'
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
processing took 0.300786 seconds.


Testing 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1
processing took 0.471874 seconds.



running testSHA256::testSum()

55

running testSHA256::testSpeedHash(array ( 0 => 10, ))

Testing ''
Start time: 2005-08-28 17:09:53
estimated time to perform test: 1.51689 seconds for 10 iterations.
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' == 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' PASSED
processing took 0.0865916 seconds.


Testing 'abc'
Start time: 2005-08-28 17:09:54
estimated time to perform test: 0.79443 seconds for 10 iterations.
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' == 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad' PASSED
processing took 0.0824555 seconds.


Testing 'message digest'
Start time: 2005-08-28 17:09:55
estimated time to perform test: 0.792 seconds for 10 iterations.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah. I had planned to put them up in a zip & tar.gz at some point, along with a live example page and tutorials on its use, but I've been busy :)

You don't require test_sha256, it's merely to help debug (1), so you can see both the amount of computations involved (2), and see if your copy is working correctly (3). It appears to be working as expected. :)

The only requirements are the first two files. Even those can be paired down to a streamlined version with just the SHA256 class, no abstract ancestor.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

yeah...now everything works fine.
I have double checked values generated by PHP and Javascript. both are the same :)
Its really a good idea to have both classes in a same file or consolidating both classes for distribution.
Thanks again feyd :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I seperated them because of the intention (and wish) for future hashing algorithms to use the abstract base. Why, you ask? Because running any hashing algorithm through a common interface means you can change which hash you are using without changing anything else. :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Code: Select all

$hashed_string = SHA256::hash($string_to_hash);
Isn't exactly portable...

Code: Select all

MyHash extends SHA256 {}

$hashed_string = MyHash::hash($string_to_hash);
Then just change the extends when we move to SHA512. ;)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

true, it's not exactly portable as is, but you can just as easily use an instance of it, in which case it is. :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

PHP4 doesn't offer easy implementation of Singletons, whereas calling a definition of a class can be done anywhere.

Ah well. You could always make a macro that went through all your php files and replaced SHA256::hash( with SHA512:::hash(
Post Reply