0
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static void main(String[] args) {
        String xmlRequest = " <request><merchant_id>46</merchant_id><order_id>33</order_id><amount>3</amount><description>hehe</description></request>";
        String result = encode(xmlRequest);
        String lol = "lol";
        String lolResult = encode(lol);
        System.out.println(result);
        System.out.println(lolResult);
    }

    public static String encode(String toEncode) {
        String hashStr = null;

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(toEncode.getBytes());
            BigInteger hash = new BigInteger(1, md.digest());
            hashStr = hash.toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return hashStr;
    }
}

Output is:

aa4c157df0d6e95c395f2376fca94c7efa35c05d

403926033d001b5279df37cbbe5287b7c7c267fa

<?
$xml='<request><merchant_id>46</merchant_id><order_id>33</order_id><amount>3</amount><description>hehe</description></request>';
$x = sha1($xml);
echo $x;
echo  sha1("lol");
?>

Output is:

d3cfa6c8ee0f7f12d28b782ac1eb45b777792e3a

403926033d001b5279df37cbbe5287b7c7c267fa

So, why string "lol" is equals and string of xml code is not?

0

1 Answer 1

3

You have an extra space - in the beginning of your java version in comparison to php, so it's not the same string

Sign up to request clarification or add additional context in comments.

1 Comment

ok, next issue: String ss = Base64.encodeBase64URLSafeString(xmlRequest.getBytes()); give me PHJlcXVlc3Q-PG1lcmNoYW50X2lkPjQ2PC9tZXJjaGFudF9pZD48b3JkZXJfaWQ-MzM8L29yZGVyX2lkPjxhbW91bnQ-MzwvYW1vdW50PjxkZXNjcmlwdGlvbj5oZWhlPC9kZXNjcmlwdGlvbj48L3JlcXVlc3Q- on PHP instead of - it have +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.