OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
16
import java.io.File;
17
import java.io.FileInputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
177 ilm 20
import java.io.OutputStream;
17 ilm 21
import java.security.DigestOutputStream;
22
import java.security.MessageDigest;
23
import java.security.NoSuchAlgorithmException;
24
 
25
public class MessageDigestUtils {
26
 
27
    private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', };
28
 
29
    /**
30
     * Turns array of bytes into string representing each byte as unsigned hex number.
31
     *
32
     * @param hash Array of bytes to convert to hex-string
33
     * @return Generated hex string
34
     */
35
    public static String asHex(byte hash[]) {
36
        return toString(hash, HEX_CHARS);
37
    }
38
 
39
    public static String toString(final byte[] hash, final char[] chars) {
40
        char buf[] = new char[hash.length * 2];
41
        for (int i = 0, x = 0; i < hash.length; i++) {
42
            buf[x++] = chars[(hash[i] >>> 4) & 0xf];
43
            buf[x++] = chars[hash[i] & 0xf];
44
        }
45
        return new String(buf);
46
    }
47
 
48
    public static byte[] fromHex(String hash) {
49
        final byte[] buf = new byte[hash.length() / 2];
50
        for (int i = 0; i < buf.length; i++) {
51
            buf[i] = (byte) Integer.parseInt(hash.substring(2 * i, 2 * i + 2), 16);
52
        }
53
        return buf;
54
    }
55
 
56
    /**
57
     * Split an int into 4 bytes.
58
     *
59
     * @param integer an int, eg -2^31, or 16.
60
     * @return the 4 bytes of its two's complement, eg [0x80, 0x00, 0x00, 0x00] or [0x00, 0x00,
61
     *         0x00, 0x10].
62
     */
63
    public static byte[] int2bytes(int integer) {
64
        final byte[] byteStr = new byte[4];
65
        byteStr[0] = (byte) (integer >>> 24);
66
        byteStr[1] = (byte) ((integer >>> 16) & 0xff);
67
        byteStr[2] = (byte) ((integer >>> 8) & 0xff);
68
        byteStr[3] = (byte) (integer & 0xff);
69
        return byteStr;
70
    }
71
 
72
    /**
73
     * Cat 4 bytes to make an int.
74
     *
75
     * @param buf an array of at least 4 bytes, eg [0x80, 0x00, 0x00, 0x03].
76
     * @return the resulting int, eg -2^31 + 3 or -2147483645.
77
     */
78
    public static int bytes2int(byte[] buf) {
79
        // eg -128 == 10000000 promoted to int => 11...110000000
80
        // so we need & 0xff to remove added ones (except for the 1st since they're all shifted
81
        // away)
82
        return (buf[0] << 24) | ((buf[1] & 0xff) << 16) | ((buf[2] & 0xff) << 8) | (buf[3] & 0xff);
83
    }
84
 
85
    public static String getHashString(MessageDigest md) {
86
        return asHex(md.digest());
87
    }
88
 
89
    public static String getHashString(String algo, byte[] data) throws NoSuchAlgorithmException {
90
        final MessageDigest md = MessageDigest.getInstance(algo);
19 ilm 91
        return getHashString(md, data);
92
    }
93
 
94
    public static String getHashString(MessageDigest md, byte[] data) {
17 ilm 95
        md.update(data);
96
        return getHashString(md);
97
    }
98
 
19 ilm 99
    public static String getHashString(MessageDigest md, final InputStream ins) throws IOException {
177 ilm 100
        return asHex(getHash(md, ins));
17 ilm 101
    }
102
 
177 ilm 103
    public static byte[] getHash(MessageDigest md, final InputStream ins) throws IOException {
104
        return getHash(md, ins, StreamUtils.NULL_OS);
105
    }
106
 
107
    public static byte[] getHash(MessageDigest md, final InputStream ins, final OutputStream out) throws IOException {
108
        try (final DigestOutputStream digestStream = new DigestOutputStream(out, md)) {
109
            StreamUtils.copy(ins, digestStream);
110
        }
111
        return md.digest();
112
    }
113
 
114
    public static byte[] getHash(MessageDigest md, final File f) throws IOException {
144 ilm 115
        try (final InputStream ins = new FileInputStream(f)) {
177 ilm 116
            return getHash(md, ins);
17 ilm 117
        }
118
    }
119
 
177 ilm 120
    public static String getMD5(File f) throws IOException {
121
        return asHex(getHash(getMD5(), f));
122
    }
123
 
19 ilm 124
    public static MessageDigest getMD5() {
125
        try {
126
            return MessageDigest.getInstance("MD5");
127
        } catch (NoSuchAlgorithmException e) {
144 ilm 128
            throw new IllegalStateException("MD5 is part of the standard JRE", e);
19 ilm 129
        }
130
    }
131
 
144 ilm 132
    public static MessageDigest getSHA256() {
133
        try {
134
            return MessageDigest.getInstance("SHA-256");
135
        } catch (NoSuchAlgorithmException e) {
136
            throw new IllegalStateException("SHA-256 is part of the standard JRE", e);
137
        }
138
    }
139
 
17 ilm 140
    private MessageDigestUtils() {
141
    }
142
 
143
}