function rsa_sign($message, $private_key, $modulus, $keylength) { $padded = add_PKCS1_padding($message, false, $keylength / 8); $number = binary_to_number($padded); $signed = pow_mod($number, $private_key, $modulus); $result = number_to_binary($signed, $keylength / 8); return $result; }
/** * * @param $message // $message is expected to be binary data * @param $private_key // $modulus should be numbers in (decimal) string format * @param $modulus // $modulus should be numbers in (decimal) string format * @param $keylength // int * @param $hash_func // name of hash function, which will be used during signing * @return $result // signature String in Base64 format */ function rsa_sign($message, $private_key, $modulus, $keylength, $hash_func) { //only suport sha1 or md5 digest now if (!function_exists($hash_func) && (strcmp($hash_func, 'sha1') == 0 || strcmp($hash_func, 'md5') == 0)) { return false; } $mssage_digest_info_hex = $hash_func($message); $mssage_digest_info_bin = hex2bin($mssage_digest_info_hex); $padded = add_PKCS1_padding($mssage_digest_info_bin, false, $keylength / 8); $number = binary_to_number($padded); $signed = pow_mod($number, $private_key, $modulus); $result = base64_encode($signed); return $result; }