Apex equivalent of PHP sha1() and some more encoding methods

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I got a PHP script to generate an encoded URL. I need to translate this script into Apex code to generate the same URL. However, the PHP encode functions seem to work differently from Apex.

The relevant PHP encoding functions are:

  1. hash("MD5", microtime())
  2. $key = sha1(sha1($var1 . $var2) . $var2);
  3. bin2hex($var1)

The code I used to generate them in Apex:

  1. String microTimeTemp = String.valueOf(DateTime.now().getTime()); Blob microtime = EncodingUtil.base64Decode(microTimeTemp); Blob hashMicrotime = Crypto.generateDigest('MD5', microtime); hashTime = EncodingUtil.base64Encode(hashMicrotime);

The closest thing I can think of to PHP microtime() is DateTime.now().getTime(). However, it seems they return different values:

in PHP microtime(): 0.85386700 1457657994

in Apex DateTime.now().getTime(): 1457658064186

Salesforce explained that the DateTime.now().getTime() returns the value of the time count since 1970.

  1. This one is the most complicated one. I actually made meaningful variables. Here I just use 1,2,3,4 for not giving real names:

String var1 = 'The String value 1'; String var2 = 'The String value 2';

String var3 = var1 + var2; Blob blob1 = Blob.valueOf(var3); Blob hash1 = Crypto.generateDigest('SHA1', blob1);

String var4 = EncodingUtil.base64Encode(hash1) + var2; Blob blob2 = Blob.valueOf(var4); Blob hash2 = Crypto.generateDigest('SHA1', blob2); String key = EncodingUtil.base64Encode(hash2);

  1. I think this one might generate the same thing as PHP code? I am not sure:

String hex = EncodingUtil.convertToHex(Blob.valueOf(var1));

When I combined them together, I am pretty sure it was not the same URL generated by the PHP script. Is there a way for Apex to generate the same URL as PHP? Also need to do something encoding as "ASCII"?

profilepic.png
manpreet 2 years ago

This is how it would look like in APEX:

String md5 = EncodingUtil.base64Encode(Crypto.generateDigest('MD5', Blob.valueOf(String.valueOf(DateTime.now().getTime()))));
system.debug(md5);

String var1 = 'The String value 1'; 
String var2 = 'The String value 2';
String sha1 = EncodingUtil.base64Encode(Crypto.generateDigest('SHA1', Blob.valueOf(EncodingUtil.base64Encode(Crypto.generateDigest('SHA1', Blob.valueOf(var1 + var2))) + var2)));
system.debug(sha1);

String bin2hex = EncodingUtil.base64Encode(Blob.valueOf(var1));
system.debug(bin2hex);

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.