Different md5 hash for same string in Swift - ios

I'm currently working on a swift application based on a particular API of a website. I use the md5 function to create valid url and access some JSON data which I can bring in my application with SwiftJSON.
I worked fine for the 6 first url but then I got an error in the checksum of my url. I checked it out and realize my md5 function in swift got the wrong hash
In my swift program, the hash of the string "answerfr1%3D%3DQf7AjOptTN6k2OyoTa7QjOptDM6k2OzoTa7cjOptjM6k2O1oTa7EjOptDMxoTa7AjOptnO2oTY"
Give me this "09938c1325c87ef89251f668a8cf5d42"
But it's not correct because my link isn't valid
But by doing it myself with http://www.md5.cz/
I have this for result "9d78b73d28f590beb8ef25b5e4b99a1d" and my link works perfectly.
I don't know why my md5 function have no issues with the 6 first hash but give me a wrong hash for the 7.
And I realize www.md5.fr give me the same wrong code. Why is there different md5 hash for the same strings ?
My md5 function in swift :
How to convert string to MD5 hash using ios swift
Hope you guys understand my issues here!
Sorry for my bad english

Thanks Martin R, you solved my problem
"%3D" is equal to "="
so "%3D%3D" is equal to "=="

Related

React native unsupported url (IOS)

Please help to solve Error: unsupported URL (IOS).When i adding data to url from reducer it not working but normally when adding url to fetch data without reducer data it error.(Same code works in Android )
There may be some value in your query string with special characters (like spaces). Try encoding the api url using encodeURI() in javascript. Or, if you are passing another url in your api query params then you may have to encode symbols which are reserved for urls (for eg: ?, :, / etc.) use encodeURIComponent() for this case. This resolved the same issue for me.
I solved with remove spaces i dont know where is space but when i get link from reducer and then replace space to empty and all solved

response checksum or hash not matching in payumoney?

When notification is passed to the app after payumoney processing it sends response hash and we need to compute the hash and match it with the passed in response hash.
I use the following code to compute the expected response hash.
Digest::SHA512.hexdigest([
PAYU_SALT,
notification.transaction_status,
notification.user_defined,
notification.customer_email,
notification.customer_first_name,
notification.product_info,
notification.gross,
notification.invoice,
PAYU_KEY].join("|"))
The hash of the following string is computed
"salt|success|||||||||||||Payment|100.0|1|key"
When I print the following hash it gives
Digest::SHA512.hexdigest([
PAYU_SALT,
notification.transaction_status,
notification.user_defined,
notification.customer_email,
notification.customer_first_name,
notification.product_info,
notification.gross,
notification.invoice,
PAYU_KEY].join("|"))
#⇒ e7b3c5ba00b98aad9186a5e6eea65028a[...]
whereas notification.checksum gives
#⇒ 546f5d23e0cadad2d4158911ef72f095d[...]
So the two hashes don’t match.
I am using the following gem: https://github.com/payu-india/payuindia
I appreciate any help as to why the response hash is not matching. Is there any error in my logic to compute the response hash? Thanks!
Where did you come up with that order for the fields in the array?
Looking at PayU's Developer FAQ it seems like the order is the following:
key|txnid|amount|productinfo|firstname|email|||||||||||salt
Please make sure that the hash is calculated in the following format - hashSequence= key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5||||||salt
Please make sure that in the above sequence please use the UDFs which have also been posted to our server. In case you haven't posted any UDFs, the hash sequence should look like this - hashSequence= key|txnid|amount|productinfo|firstname|email|||||||||||salt.
Keep in mind that when computing the hash even a single character out of place will result in a completely different checksum.
little late but Actual Sequence is:
SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key
Thanks to Ravi Kant Singh
but additionalCharges| are removed
Tested with live environment
Check your hash in above order and if its match you can process request
ok this was a silly mistake i made. The reason the hash didn't match was beacuse i had a typo with the PAYU test key. At the end i typed small 'u' when it was 'U'. The library is fine and the logic is right. The error was in my side with using wrong key.
Actual Sequence for hash is :
additionalCharges|SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key
Actual hash generation for additional charges:
additionalCharges|SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key
Without additional charges:
SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key

how to use md5 format retrieved from json in iOS app

I've never used md5 format and i don't know how to decode it to get the string that is in response. How can i get the string from the md5 format?
I'm using swift language on Xcode for iOS app development. If there is any library that can be used please do suggest it.
TIA
That's why its MD5 hashing. You cannot decode it. It is more secured than simple string. Few years ago all passwords were encoded to MD5 because of security.
You can't decode MD5. What you can do instead is comparing the MD5 string from your JSON with a MD5(stringInYourApp). That means that if you want to compare the user input (for example) and the MD5 from your JSON you'll have to :
Make a MD5 version of user input string (see here for how to)
Compare it with the MD5 from JSON
You cannot see the original string from a MD5 hash.

Datausingencoding that doesn't replace plus signs

I'm looking for a datausingencoding parameter that doesn't swallow up plus signs. I was using NSASCIIENCODING but since I'm trying to send a uiimage to the server and the base64 string had plus signs in them, it seems like that form of encoding takes out the plus sign sending a modified encoded string to the server thereby not allowing the image to be decoded server side. I'm looking for something that won't alter the base64 string.
Nevermind guys, here is the solution I found on stackoverflow
thanks, now I figured it out. It seems I needed to run my string through the stringByAddingPercentEscapesUsingEncoding: first, then I needed to run it through replaceOccurrencesOfString:#"+" withString:#"%2B" and several more of those replaces for different characters, because stringByAddingPercentEscapesUsingEncoding: doesn't escape them all

SHA256 implementation using Base64 for input and output

I've been asked to develop the company's backoffice for the iPad and, while developing the login screen, I've ran into an issue with the authentication process.
The passwords are concatenated with a salt, hashed using SHA-256 and stored in the database.
The backoffice is Flash-based and uses the as3crypto library to hash then password+salt and my problem is that the current implementation uses Base64 for both input and output.
This site demonstrates how this can be done: just select Hash and select Base64 for both input and output format and fire away. So far, all my attempts have yielded different results from the ones this site (and the backoffice code) give me.
While I think that in theory it should be relatively simply:
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
so far I haven't been able to do this and I'm getting quite the headache to be honest.
My code is becoming a living maze, i'll have to redo-it tomorrow I reckon.
Any ideas?
Cheers and thanks in advance
PS: Here's the Backoffice's Flash code for generating hashed passwords by the way:
var currentResult:ByteArray;
var hash:IHash = Crypto.getHash('sha256');
var data:ByteArray = Base64.decodeToByteArray(str + vatel);
currentResult = hash.hash(data);
return Base64.encodeByteArray(currentResult).toString();
The backoffice code does not do
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
(as you wrote above)
Instead, what it does is
Base64 decode the pass+salt string into a byte array
Hash the byte array using SHA-256
Base64 encode the byte array, returning a string
As per step 1 above, it's a unclear what kind of character encoding the input strings uses. You need to make sure that both systems use the same encoding for the input strings! UTF8, UTF16-LE or UTF16-BE makes a world of a difference in this case!
Start by finding out the correct character encoding to use on the iOS side.
Oh, and Matt Gallagher has written an easy to use wrapper class for hashes to use on iOS, HashValue.m, I've used it with good results.

Resources