开发者

How can I get md5 sum from date in JAVA such in oracle?

I have a next trouble. In database date storred as md5 from date. When I make md5 from same date in JAVA i had another hash. For example: date - 01.02.1980 java hash is addaf0f9a1fb3699871293f888f6e46e (same as oracle md5 hash from string) oracle hash is DF0919EA828A77DC2CCF68474ED703AC

I tried to repeat oracle result in java:

System.out.println(streamConvertor.getHash("01.02.1980", Boolean.TRUE));
System.out.println("");
System.out.println(streamConvertor.getHash("01.02.1980", Boolean.FALSE));

public String getMD5(String input, Boolean is_date) throws ParseException {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] mdinput;
        Boolean pr;
        if (input.equals("01.02.1980")) {
            pr = true;
        } else {
            pr = false;
        }

        if (is_date == Boolean.TRUE) {
            SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
            Date day = df.parse(input);  
            mdinput = df.format(day).getBytes();
        } else {
            mdinput = input.getBytes();
        }
        if (pr == Boolean.TRUE) {
            System.out.println("Is date " + is_date + " - " + mdinput);
        }
        byte[] messageDigest = md.digest(mdinput);开发者_StackOverflow中文版
        if (pr == Boolean.TRUE) {
            System.out.println("message - " + messageDigest);
        }
        BigInteger number = new BigInteger(1, messageDigest);
        if (pr == Boolean.TRUE) {
            System.out.println("Number - " + number);
        }
        String hashtext = number.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        if (pr == Boolean.TRUE) {
            System.out.println("hashtext - " + hashtext);
        }
        return hashtext;
    }
    catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

and output is different, but only until byte[] converted to BigInteger:

Is date true - [B@4a65e0
message - [B@665753
Number - 231093251542199165689934828427325924462
hashtext - addaf0f9a1fb3699871293f888f6e46e

Is date false - [B@ef22f8
message - [B@1e0cf70
Number - 231093251542199165689934828427325924462
hashtext - addaf0f9a1fb3699871293f888f6e46e

Is there any possibilities make md5 hash such as in oracle?


In your comment above, you say the exact expression used in Oracle was:

DBMS_CRYPTO.hash( UTL_RAW.CAST_TO_RAW( UPPER(to_date('01.02.1980', 'dd.mm.yyyy')) ),2 )

So you take the string '01.02.1980' and convert it to a DATE using an appropriate format. Then you pass the result of that to UPPER(); this will cause the DATE to be implicitly converted to a string, using the default date format for the session. So the result of this expression can potentially be different if it is executed in different environments.

To try to reproduce the same hash value in Java, use the date format indicated by your NLS_DATE_FORMAT instance parameter. But it is possible that would work for some entries but not others, since the date format used could have varied.

Basically, you have a very bad bug in the Oracle code that created and stored these hash values. Implicit conversion of a date to a string can be dangerous.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜