identical md5 for JS and PHP - php

for a small project I need identical md5 hashes for both JS and PHP.
Im using this javascript to convert a word to md5 (the .min version)
the word 'hello' outputs in
JS: ec59d44dee488759467970486fc9402d
PHP: 5d41402abc4b2a76b9719d911017c592
so they are not identical. I've tried to use md5(utf8_encode($word)); instead of md5($word);
but both gave the same result.
Any ideas? Help is much appreciated

The md5 function on phpjs gives the correct results. It is dependent on utf8_encode, so you need that as well.

If you want to generate md5 value in Javascript, you may use this md5 package
var md5 = require('md5');
console.log(md5('message'));
Then, use this md5 online tool to verify the result.

Related

PHP short encrypt

I'm using this code:
$url = "http://www.webtoolkit.info/javascript-base64.html";
print base64_encode($url);
But the result is very long: "aHR0cDovL3d3dy53ZWJ0b29sa2l0LmluZm8vamF2YXNjcmlwdC1iYXNlNjQuaHRtbA=="
There is a way to transform long string to short encryption and to be able to transform?
for example:
new_encrypt("http://www.webtoolkit.info/javascript-base64.html")
Result: "431ASDFafk2"
encoding is not encrypting. If you're depending on this for security then you're in for a very nasty shock in the future.
Base 64 encoding is intended for converting data that's 8 bits wide into a format that can be sent over a communications channel that uses 6 or 7 bits without loss of data. As 6 bits is less than 8 bits the encoded string is obviously going to be longer than the original.
This q/a might have what you're looking for:
An efficient compression algorithm for short text strings
It actually links here:
http://github.com/antirez/smaz/tree/master
I did not test it, just found the links.
First off, base64 is an encoding standard and it is not meant to encrypt data, so don't use that. The reason your data is so much longer is that for every 6 bits in the input string, base64 will output 8 bits.
There is no form of encryption that will directly output a shortened string. The result will be just as long in the best case.
A solution to that problem would be to gzip your string and then encrypt it, but with your URL the added data for the zip format will still end up making your output longer than the input.
There are a many different algorithms for encrypting/decryption. You can take a look at the following documentation: http://www.php.net/manual/en/function.mcrypt-list-algorithms.php (this uses mcrypt with different algorithms).
...BUT, you can't force something to be really small (depends on the size you want). The encrypted string needs to have all the information available to be able to decrypt it. Anyways, a base64-string is not that long (compared with really secure salted hashes for example).
I don't see the problem.
Well... you could try using md5() or uniqid().
The first one generate the md5 hash of your string.
md5("http://www.webtoolkit.info/javascript-base64.html");
http://php.net/manual/en/function.md5.php
The second one generates a 13 unique id and then you can create a relation between your string and that id.
http://php.net/manual/en/function.uniqid.php
P.S. I'm not sure of what you want to achieve but these solutions will probably satisfy you.
You can be creative and just do some 'stuff' to encrypt the url so that it is not easy quess able but encode / decode able..
like reverse strings...
or have a random 3 letters, your string encoded with base64 or just replace letters for numbers or numbers for letters and then 3 more random letters.. once you know the recipe, you can do and undo it.
$keychars = "abcdefghijklmnopqrstuvwxyz0123456789";
$length = 2;
$randkey = "";
$randkey2 = "";
for ($i=0;$i<$length;$i++) $randkey .= substr($keychars, rand(1, strlen($keychars) ), 1);

sha1 encode exact value of parameter instead of the variable value

I'm working with an application that requires sha1 encoding for certain form values.
The problem is that when I use the following
<?php echo(hash("sha1","par1=".$_POST['p1']."&par2=".$_POST['p2'])); ?>
It gives me a sha1 encoding of the actual string, while I want to get a sha1 encoding of the posted values, so in this example I want to get
<?php echo(hash("sha1","par1=firstvalue&par2=secondvalue")); ?>
How can I realize this? Is it actually that simple and am I thinking way to difficult?
That because it identifies that para1=some_value as string not para1 as variable and some_value string
To achieve what you want you should hash every variable alone
Or I suggest that you implement your own encoding algorithm
Why not do it like this? Though I would have though either way would result in the same thing....
$hash_this = "par1=".$_POST['p1']."&par2=".$_POST['p2'];
echo sha1($hash_this);
Though that will do it if you want to hash the string of the values all together, rather than the values, if you want to store the values - then you should probably hash each value, so you could at least compare them later. Useful for a login system where you want to save a password to a database, which is more secure than literally just storing the password...
$password = sha1($_POST['password']);
If you hash the string, you have no idea which value is wrong

blowfish encryption in actionscript 3

I want to mask a string value that I have in my embed code. I thought I could encrypt it using Blowfish or something similar. I don't care that is not secure. Basically I don't want someone to copy the encrypted string from my embed code and get the result. So I thought adding some salt would make it harder. At least they would have to decompile my swf in order to get the salt.
I looked at google code for as3cryto and can't seem to figure out how to write it. I suspect I have to import the class...not sure which class to import and not sure as well what the syntax would be to call the blowfish encrypt and decrypt functions.
Are there any examples out there for as3 blowfish decrypt and encrypt? I searched and only found people modifying and writing their own classes based on as3crypto. I'm sure it is one simple line but I don't see any examples to pin it down.
I will be using php to generate the encryption string and then use as3 to decrypt it to get the string I need to use in my flash file.
Thanks.
I'm not really up on this stuff, but it seems pretty straightforward, if you read this class:
http://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/com/hurlant/crypto/symmetric/BlowFishKey.as
The import and declaration would look something like this. It's obviously missing the key info:
import com.hurlant.crypto.symmetric.BlowFishKey;
var key:ByteArray = new ByteArray()
var bfKey:BlowFishKey = new BlowFishKey(key);
Here is an example at Github of an implementation of the BlowFishKey class:
https://github.com/jeromeetienne/EasyWebsocket/blob/aa333e059b92c9441bc22b5a84be7ec51008f3d4/node/server/node_modules/socket.io/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/tests/BlowFishKeyTest.as
If you're lazy like me, just convert your string to the ascii values and use String.fromCharCode()
trace( String.fromCharCode( 72,69,76,76,79,32,87,79,82,76,68 ) ); // hello world
After that, you can store the numbers how you want

Calculating SHA1 in PHP similar to CC_SHA1_Update in C

So I have an application in C that is transmitting data over the internet to a PHP page. The C implementation for my hash is:
CC_SHA1_CTX ctx;
CC_SHA1_Init(&ctx);
CC_SHA1_Update(&ctx, ciph_right,20);
CC_SHA1_Update(&ctx, _keyRight, 20);
CC_SHA1_Final(digest, &ctx);
Where ciph_right and _keyRight are simply 2 byte arrays of length 20. How can I achieve this similar implementation for PHP so I'm getting the same result?
I don't hav the CC_SHA1_Update function in PHP, I simply have sha1(). And unfortunately I'm not 100% certain what CC_SHA1_Update actually does. I thought it simply combined the two byte arrays + took a hash of them together, but this appears to not be the case.
Anyone have any ideas?
Thanks!
The sha1 function takes a complete string and derives a SHA1 hash from that complete string.
A bit of Googling suggests that the C calls to CC_SHA1_Update are just adding more data to be hashed.
You therefore have two options.
Just concatenate all the data together and then call sha1 on it.
Use the (standard) hash extension in streaming mode:
.
$h = hash_init('sha1');
hash_update($h, 'some data');
hash_update($h, 'some more data');
$final_hash = hash_final($h);
$final_hash should then contain fa978155004254c23f9cf42918ad372038afcaf5, which happens to be the same hash as the string 'some datasome more data'.

How do I encode something in PHP with a key?

I see base64_encode can encode values, but without a key. I need a function that takes a salt and data, encodes it, and can be decoded with the same salt (but if you try to decode it without the salt, it gives you gibberish).
Is there a PHP function for this (can't find it, only modified versions of base64_encode).
EDIT: Found the answer: use mcrypt ciphers
What about taking a look at http://www.php.net/manual/en/book.mcrypt.php

Categories