Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
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.
Turn Your Knowledge into Earnings.
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:
The code I used to generate them in Apex:
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:
DateTime.now().getTime().
in PHP microtime(): 0.85386700 1457657994 in Apex DateTime.now().getTime(): 1457658064186
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.
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);
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"?
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);
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.
General Tech 10 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 9 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.