Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created December 3, 2009 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/247871 to your computer and use it in GitHub Desktop.
Save antimatter15/247871 to your computer and use it in GitHub Desktop.
<?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";
*/
@iremclk
Copy link

iremclk commented Mar 30, 2019

Is "$n" calculate hash of message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment