Created
December 3, 2009 03:49
-
-
Save antimatter15/247871 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
/* | |
Written by Antimatter15. Released to Public Domain. | |
Changes: | |
December 3 - Created | |
December 5 - Fixed bug (undefined bmadd), Switched to raw SHA-1 (allow < 512 bit keys), Fixed Adding, Undefined variable, Uninitialized String | |
*/ | |
/* | |
Parameters: | |
$m - Message | |
$e - Encryption Exponent | |
$n - Modulo | |
Returns: | |
string signature | |
*/ | |
function sign($m, $e, $n) { | |
$m=sha1($m, true); | |
$c=""; | |
for($j=0; $j<20; $j++){ | |
$c = bcadd($c, bcmul(ord($m[$j]), bcpow('256',$j))); | |
} | |
return bcpowmod($c, $e, $n); | |
} | |
/* | |
Parameters: | |
$m - Message | |
$s - Signature | |
$d - Decryption Exponent | |
$n - Modulo | |
Returns: | |
Boolean passes verification or not | |
*/ | |
function verify($m, $s, $d, $n) { | |
$t = ''; | |
$c = bcpowmod($s, $d, $n); | |
while(bccomp($c, '0') != 0){ | |
$t .= chr(bcmod($c, '256')); | |
$c = bcdiv($c, '256', 0); | |
} | |
return $t==sha1($m, true); | |
} | |
/* | |
$e = "3514154152523482244510025679905027432832717686149733933270841589963848086479"; | |
$d = "1345113334183320009999299256137330751442566269958512189873637535318199721119"; | |
$n = "4876682701309516951234725500059641167033512755752338611478313046145089995111"; | |
$msg = "Woot I am a poop"; | |
$sgn = sign($msg, $e, $n); | |
echo "Signature: ", $sgn,"\n"; | |
echo verify($msg, $sgn, $d, $n)?"WOOT":"POOP","\n"; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is "$n" calculate hash of message?