SHA1 with BASE64 in java util class does not generate the correct password

I wrote a util class in Java to call webservice. My util class creates the password digest needed to invoke the web service. This digest password consists of: a digest password generated using the following algorithm:base64Encode(sha1Hash(<Nonce><TimeStamp><Secret>))

My generated password is not equal to the generated password from a provider tool that uses the same algorithm (I do not have access to their code, so I'm not sure how this is implemented). I'm not sure that I did something wrong, can someone look at my code and see if I did something wrong with SHA1 encryption or Base64 encoding. Thank you for your help! Below is my code:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.UUID;

import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.binary.Base64;

public class OminitureWSUtil {

private static MessageDigest SHA1;

static {
    try {
        SHA1 = MessageDigest.getInstance("SHA1");

    } catch(NoSuchAlgorithmException nae) {
        throw new RuntimeException(nae);
    }
}

static class OmniturePasswordDigest {
    private final String timestamp;
    private final String nonce;
    private final String secret;

    private String password;

    public OmniturePasswordDigest(String secret) {
        Calendar c = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT+0"));
        c.setTime(new Date());

        //timestamp =  DatatypeConverter.printDateTime(c);
        //nonce = UUID.randomUUID().toString().replace("-", "");

        timestamp = "2011-01-26T20:10:56Z";
        nonce = "MTkyMTYwZWMzMjIzZGJmYzNiYmE5M2E5";

        this.secret = secret;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public String getNonce() {
        return nonce;
    }

    public String generatePassword() {
        if(password == null) {
            String beforeEncryption = nonce+timestamp+secret;
            System.out.println("before encryption, encoding: " + beforeEncryption);

            try {
                SHA1.reset();
                byte[] toEncrypt = beforeEncryption.getBytes("UTF-8");
                //SHA1.update(toEncrypt, 0, toEncrypt.length);
                SHA1.update(beforeEncryption.getBytes());
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException(uee);
            }

            byte[] encryptedRaw = SHA1.digest();
            byte[] encoded = Base64.encodeBase64(encryptedRaw);

            try {
                password = new String(encoded, "UTF-8");
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException(uee);
            }
        }

        return password;
    }
}


public static OmniturePasswordDigest generatePasswordDigest(String secret) {
    return new OmniturePasswordDigest(secret);
}

public static void main(String[] args) {
    OmniturePasswordDigest opd = generatePasswordDigest("1779ab07fb93a01e3d4a6ee174124b91");
    System.out.println("nonce: " + opd.getNonce());
    System.out.println("timestamp: " + opd.getTimestamp());
    System.out.println("password: " + opd.generatePassword());

    if("Lr+m+/6y3XUxvjd8Rtn75gqn/b4=".equals(opd.generatePassword())) {
        System.out.println("all good");
    } else {
        System.out.println("generated password is not the same, it should be: " + 
                "Lr+m+/6y3XUxvjd8Rtn75gqn/b4=");
    }

}

}

+3
source share
2 answers

SHA1 . :

-SHA1.reset();
+MessageDigest SHA1= (MessageDigest) OminitureWSUtil.SHA1.clone();

Reset , ; .

btw, init (static {}) , ( [-]). , (java.lang.ExceptionInInitializerError) - .

+2

, REST Api Omniture? . .

https://developer.omniture.com/java_rest_api

, , SHA1 , generatePassword(), - .

0

Source: https://habr.com/ru/post/1788052/


All Articles