C
C
ChernovGV2015-09-09 05:42:29
PHP
ChernovGV, 2015-09-09 05:42:29

How to generate a hash according to GOST (there is a code in JAVA, you need it in php)?

Good afternoon.
The task is to send files using the PUT method to a specific server, and then transfer the hash of this file via SOAP.
The documentation says that it is required to hash according to the GOST 34.11-94 algorithm.
Tried like this:

$string = file_get_contents($path);
$hash = bin2hex(mhash(MHASH_GOST,$string));

But when sending a SOAP envelope, an error occurs saying that the hash is not correct.
I contacted support, they gave me an example of hashing in java:
byte[] bytes = IOUtils.toByteArray(new FileInputStream("/home/zinin/tmp/xxx.pdf"));
        MessageDigest digest = MessageDigest.getInstance(GostConsts.GOST_DIGEST_ALGORITHM_NAME, CommonConstants.TRUSTED_JAVA_PROVIDER);
        byte[] result = digest.digest(bytes);
        LOGGER.info("hash: " + new String(Hex.encodeHex(result, true)));

I also calculated the hash for the string "somestring" = "5ed5fe405f6872d23fb71ce21321bcb514e1fe59bdd9f21e3bc1d3fe3c48d758";
Calculate the hash in php:
$string = "somestring";
echo bin2hex(mhash(MHASH_GOST,$string));
//Результат:66231654633f311301af1c9eb192e5a7e85f0a2036ce37a1bce7d1c502f009bf
echo hash('gost', $string);
//Такой же результат

Which is not true.
I tried to generate a hash using the rhash utility:
The RHash program supports the calculation of GOST R 34.11-94 with test and CryptoPro parameters and both hash output formats (low bytes first by default). Call examples:
rhash --gost empty-file.txt
rhash --gost --gost-reverse empty-file.txt
rhash --gost-cryptopro empty-file.txt
rhash --gost-cryptopro --gost-reverse empty- file.txt

But I did not get the desired result, although I tried all these examples. By the way, the first option considers the same hash as the given php code.
In general, I do not know which way to dig. Tell me how to implement the above mechanism in java using php?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-09-09
@ChernovGV

In php 5.6 should work:
echo hash('gost-crypto', 'somestring');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question