Example usage for org.apache.commons.codec.digest DigestUtils getMd5Digest

List of usage examples for org.apache.commons.codec.digest DigestUtils getMd5Digest

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils getMd5Digest.

Prototype

public static MessageDigest getMd5Digest() 

Source Link

Usage

From source file:at.creadoo.util.netio.NetIO.java

private String getPasswordHash() throws NoSuchAlgorithmException, UnsupportedEncodingException {
    return Hex.encodeHexString(
            (DigestUtils.getMd5Digest().digest((user + pass + hash).getBytes(Constants.DEFAULT_CHARSET))));
}

From source file:nl.opengeogroep.filesetsync.FileRecord.java

public static String calculateHashMappedIO(File f) throws FileNotFoundException, IOException {
    try (RandomAccessFile raf = new RandomAccessFile(f, "r"); FileChannel channel = raf.getChannel();) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        buffer.load();/*from ww w  .ja v a2s.  co  m*/

        MessageDigest md = DigestUtils.getMd5Digest();
        md.update(buffer);
        byte[] digest = md.digest();

        return Hex.encodeHexString(digest);
    }
}

From source file:org.ancoron.hazelcast.rest.osgi.HazelcastServiceTest.java

@Test
public void lifecycleSimple() throws Exception {
    Config cfg = new Config("HazelcastServiceTest");

    // disable multicast...
    cfg.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);

    HazelcastInstance hz = Hazelcast.newHazelcastInstance(cfg);

    HazelcastMapServlet service = new HazelcastMapServlet();
    service.setHazelcast(hz);/*from   ww w  .  ja va  2s. c o  m*/

    // Step #1: create a bucket
    service.createBucket("A", 60, 0, 128);

    // Step #2: insert some data for a key
    MessageDigest md5 = DigestUtils.getMd5Digest();
    MessageDigest sha1 = DigestUtils.getSha1Digest();
    try (InputStream testData = new DigestInputStream(new DigestInputStream(stream("test.json"), sha1), md5)) {
        service.setValue("A", "1", "application/json", 171, testData);
    }

    // Step #3: fetch some data for a key
    byte[] value = service.getValue("A", "1");
    byte[] data = new byte[value.length - 1];
    System.arraycopy(value, 1, data, 0, data.length);

    Assert.assertThat(DigestUtils.md5Hex(data), CoreMatchers.is(Hex.encodeHexString(md5.digest())));
    Assert.assertThat(DigestUtils.sha1Hex(data), CoreMatchers.is(Hex.encodeHexString(sha1.digest())));

    // Step #4: delete the bucket
    service.deleteBucket("A");
}

From source file:org.ensembl.gti.seqstore.utils.SeqStoreHashUtils.java

public static String hashGeneModel(Gene gene) {
    MessageDigest digest = DigestUtils.getMd5Digest();
    digest.update(locationToString(gene).getBytes());
    for (Transcript transcript : gene.getTranscripts()) {
        digest.update(hashTranscriptModel(transcript).getBytes());
    }//w ww.  ja  v a2s  .  c om
    return Hex.encodeHexString(digest.digest());
}

From source file:org.ensembl.gti.seqstore.utils.SeqStoreHashUtils.java

public static String hashTranscriptModel(Transcript transcript) {
    MessageDigest digest = DigestUtils.getMd5Digest();
    digest.update(locationToString(transcript).getBytes());
    for (Exon exon : transcript.getExons()) {
        digest.update(hashExonModel(exon).getBytes());
    }/*from   w w  w . j  ava  2  s .com*/
    return Hex.encodeHexString(digest.digest());
}

From source file:org.nuxeo.launcher.config.ConfigurationGenerator.java

private void writeConfiguration() throws ConfigurationException {
    final MessageDigest newContentDigest = DigestUtils.getMd5Digest();
    StringWriter newContent = new StringWriter() {
        @Override/*www . ja  v a 2  s .c  o m*/
        public void write(String str) {
            if (str != null) {
                newContentDigest.update(str.getBytes());
            }
            super.write(str);
        }
    };
    // Copy back file content
    newContent.append(readConfiguration());
    // Write changed parameters
    newContent.write(BOUNDARY_BEGIN + System.getProperty("line.separator"));
    for (Object o : new TreeSet<>(userConfig.keySet())) {
        String key = (String) o;
        // Ignore parameters already stored in newContent
        if (PARAM_FORCE_GENERATION.equals(key) || PARAM_WIZARD_DONE.equals(key)
                || PARAM_TEMPLATES_NAME.equals(key)) {
            continue;
        }
        String oldValue = storedConfig.getProperty(key, "");
        String newValue = userConfig.getRawProperty(key, "");
        if (!newValue.equals(oldValue)) {
            newContent.write("#" + key + "=" + oldValue + System.getProperty("line.separator"));
            newContent.write(key + "=" + newValue + System.getProperty("line.separator"));
        }
    }
    newContent.write(BOUNDARY_END + System.getProperty("line.separator"));

    // Write file only if content has changed
    if (!Hex.encodeHexString(newContentDigest.digest()).equals(currentConfigurationDigest)) {
        try (Writer writer = new FileWriter(nuxeoConf, false)) {
            writer.append(newContent.getBuffer());
        } catch (IOException e) {
            throw new ConfigurationException("Error writing in " + nuxeoConf, e);
        }
    }
}

From source file:org.nuxeo.launcher.config.ConfigurationGenerator.java

private StringBuffer readConfiguration() throws ConfigurationException {
    String wizardParam = null, templatesParam = null;
    Integer generationIndex = null, wizardIndex = null, templatesIndex = null;
    // Will change wizardParam value instead of appending it
    wizardParam = userConfig.getProperty(PARAM_WIZARD_DONE);
    // Will change templatesParam value instead of appending it
    templatesParam = userConfig.getProperty(PARAM_TEMPLATES_NAME);
    ArrayList<String> newLines = new ArrayList<>();
    try (BufferedReader reader = new BufferedReader(new FileReader(nuxeoConf))) {
        String line;//w ww .  j  a v a 2s . c  o  m
        MessageDigest digest = DigestUtils.getMd5Digest();
        boolean onConfiguratorContent = false;
        while ((line = reader.readLine()) != null) {
            digest.update(line.getBytes());
            if (!onConfiguratorContent) {
                if (!line.startsWith(BOUNDARY_BEGIN)) {
                    if (line.startsWith(PARAM_FORCE_GENERATION)) {
                        if (setOnceToFalse && onceGeneration) {
                            line = PARAM_FORCE_GENERATION + "=false";
                        }
                        if (setFalseToOnce && !forceGeneration) {
                            line = PARAM_FORCE_GENERATION + "=once";
                        }
                        if (generationIndex == null) {
                            newLines.add(line);
                            generationIndex = newLines.size() - 1;
                        } else {
                            newLines.set(generationIndex, line);
                        }
                    } else if (line.startsWith(PARAM_WIZARD_DONE)) {
                        if (wizardParam != null) {
                            line = PARAM_WIZARD_DONE + "=" + wizardParam;
                        }
                        if (wizardIndex == null) {
                            newLines.add(line);
                            wizardIndex = newLines.size() - 1;
                        } else {
                            newLines.set(wizardIndex, line);
                        }
                    } else if (line.startsWith(PARAM_TEMPLATES_NAME)) {
                        if (templatesParam != null) {
                            line = PARAM_TEMPLATES_NAME + "=" + templatesParam;
                        }
                        if (templatesIndex == null) {
                            newLines.add(line);
                            templatesIndex = newLines.size() - 1;
                        } else {
                            newLines.set(templatesIndex, line);
                        }
                    } else {
                        int equalIdx = line.indexOf("=");
                        if (equalIdx < 1 || line.trim().startsWith("#")) {
                            newLines.add(line);
                        } else {
                            String key = line.substring(0, equalIdx).trim();
                            if (userConfig.getProperty(key) != null) {
                                newLines.add(line);
                            } else {
                                newLines.add("#" + line);
                            }
                        }
                    }
                } else {
                    // What must be written just before the BOUNDARY_BEGIN
                    if (templatesIndex == null && templatesParam != null) {
                        newLines.add(PARAM_TEMPLATES_NAME + "=" + templatesParam);
                        templatesIndex = newLines.size() - 1;
                    }
                    if (wizardIndex == null && wizardParam != null) {
                        newLines.add(PARAM_WIZARD_DONE + "=" + wizardParam);
                        wizardIndex = newLines.size() - 1;
                    }
                    onConfiguratorContent = true;
                }
            } else {
                if (!line.startsWith(BOUNDARY_END)) {
                    int equalIdx = line.indexOf("=");
                    if (line.startsWith("#" + PARAM_TEMPLATES_NAME) || line.startsWith(PARAM_TEMPLATES_NAME)) {
                        // Backward compliance, it must be ignored
                        continue;
                    }
                    if (equalIdx < 1) { // Ignore non-readable lines
                        continue;
                    }
                    if (line.trim().startsWith("#")) {
                        String key = line.substring(1, equalIdx).trim();
                        String value = line.substring(equalIdx + 1).trim();
                        getStoredConfig().setProperty(key, value);
                    } else {
                        String key = line.substring(0, equalIdx).trim();
                        String value = line.substring(equalIdx + 1).trim();
                        if (!value.equals(userConfig.getRawProperty(key))) {
                            getStoredConfig().setProperty(key, value);
                        }
                    }
                } else {
                    onConfiguratorContent = false;
                }
            }
        }
        reader.close();
        currentConfigurationDigest = Hex.encodeHexString(digest.digest());
    } catch (IOException e) {
        throw new ConfigurationException("Error reading " + nuxeoConf, e);
    }
    StringBuffer newContent = new StringBuffer();
    for (int i = 0; i < newLines.size(); i++) {
        newContent.append(newLines.get(i).trim() + System.getProperty("line.separator"));
    }
    return newContent;
}

From source file:org.sonar.batch.scan.filesystem.FileHashDigest.java

/**
 * Compute hash of a file ignoring line ends differences.
 * Maximum performance is needed.// ww  w  . jav a  2  s  .  c  om
 */
String hash(File file, Charset charset) {
    Reader reader = null;
    try {
        MessageDigest md5Digest = DigestUtils.getMd5Digest();
        md5Digest.reset();
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
        int i = reader.read();
        boolean afterCR = true;
        while (i != -1) {
            char c = (char) i;
            if (afterCR) {
                afterCR = false;
                if (c == '\n') {
                    // Ignore
                    i = reader.read();
                    continue;
                }
            }
            if (c == '\r') {
                afterCR = true;
                c = '\n';
            }
            md5Digest.update(charToBytesUTF(c));
            i = reader.read();
        }
        return Hex.encodeHexString(md5Digest.digest());
    } catch (IOException e) {
        throw new IllegalStateException(String.format("Fail to compute hash of file %s with charset %s",
                file.getAbsolutePath(), charset), e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.sonar.batch.scan.filesystem.FileMetadata.java

/**
 * Compute hash of a file ignoring line ends differences.
 * Maximum performance is needed./*ww  w .j av a  2  s . com*/
 */
Metadata read(File file, Charset encoding) {
    Reader reader = null;
    int lines = 0;
    char c = (char) -1;
    try {
        MessageDigest md5Digest = DigestUtils.getMd5Digest();
        md5Digest.reset();
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        int i = reader.read();
        boolean afterCR = false;
        while (i != -1) {
            c = (char) i;
            if (afterCR) {
                afterCR = false;
                if (c == LINE_FEED) {
                    // Ignore
                    i = reader.read();
                    continue;
                }
            }
            if (c == CARRIAGE_RETURN) {
                afterCR = true;
                c = LINE_FEED;
            }
            if (c == LINE_FEED) {
                lines++;
            }
            md5Digest.update(charToBytesUTF(c));
            i = reader.read();
        }
        if (c != (char) -1) {
            lines++;
        }
        String hash = Hex.encodeHexString(md5Digest.digest());
        return new Metadata(lines, hash);

    } catch (IOException e) {
        throw new IllegalStateException(
                String.format("Fail to read file '%s' with encoding '%s'", file.getAbsolutePath(), encoding),
                e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.sonar.server.platform.db.migration.version.v64.UpgradeQualityTemplateLoadedTemplates.java

@Override
protected void execute(Context context) throws SQLException {
    String defaultOrganizationUuid = this.defaultOrganizationUuid.getAndCheck(context);

    MassUpdate massUpdate = context.prepareMassUpdate();
    massUpdate.select("select id,kee from loaded_templates where template_type=?").setString(1,
            QUALITY_PROFILE_TYPE);/*  www  . j  a v  a 2 s  .com*/
    massUpdate.rowPluralName("loaded templates for quality profiles");
    massUpdate.update("update loaded_templates set template_type=?,kee=? where id=?");
    MessageDigest md5Digest = DigestUtils.getMd5Digest();
    massUpdate.execute((row, update) -> {
        int id = row.getInt(1);
        String key = row.getString(2);

        update.setString(1, computeLoadedTemplateType(key, md5Digest));
        update.setString(2, defaultOrganizationUuid);
        update.setInt(3, id);
        return true;
    });
}