views:

125

answers:

2

Hello developers,

I'm going to implement license key generator on my website but I've very few knowledges of php! In my (Cocoa) application I've integrate a system verification that use the RSA_sign function (in C#) ... I would like to use the paypal IPN notification system to automatically generate and send to my users their license keys ... but I really don't know how to sign a string in php using an RSA key!!! In php I know only the openss_sign function but it isn't the same thing! Or is better and easiest to use a DSA signing method???

Please, help!

Thanks in advance for all the replies!!!

A: 

You want openssl_sign().

EDIT:

  • Make sure the message digest is the one you want to use to verify.
  • Don't test it by comparing the base64 output with another signature, test it by verifying the signature in your C# code. I believe it's possible for two valid signatures with the same key of the same data to have differing binary representations.
caf
but I've tried to sign the same string before with RSA_sign in C# and then with openss_sign in PHP . ... and the base64 representation is different!! If you want I post my code!!!
I've posted the hex output of the SHA1 to be more objective! There is some mistake?
A: 

Thank you! You are right!

I've found the problem, is in the hash string that I use with the openssl_sign! I've this code in objective-c:

NSString *stringToEncript = @"test";

const char *clearText = [stringToEncript UTF8String];

unsigned char md[SHA_DIGEST_LENGTH];
SHA1((unsigned char *)clearText, strlen(clearText), md);

for(int i=0; i<SHA_DIGEST_LENGTH; i++)
    printf("%x", md[i]);

and in php I've written:

$stringToEncrypt = "test";

$hash = sha1(utf8_encode($stringToEncrypt));
echo $hash;

The output of the first code is:

a94a8fe5ccb19ba61c4c873d391e987982fbbd3

The output of the second code is:

a94a8fe5ccb19ba61c4c0873d391e987982fbbd3

As you can see there is a '0' missed! What do I wrong?

If you use "%02x" as your printf string you'll get what you expect there.I think I see the problem, though. openssl_sign() does the SHA1 hash for you - you just pass it the $stringToEncrypt. If you do the hash yourself it'll end up signing the hash-of-the-hash.
caf
Ok! So what I did wrong was the output!But, when I generate the signed string in binary encode, there is a way, in php, to create a .plist file in whitch the signed string is an NSData object? Or I must make a base64 encode of the signed string and then store it as an NSString object in the plist file?
PHP can output binary, sure. But outputting a base64 version is probably going to be easier to parse and easier to debug.
caf
Very very thaks caf!