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

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

Introduction

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

Prototype

public static String sha1Hex(String data) 

Source Link

Usage

From source file:org.sead.nds.repository.BagGenerator.java

public static File getBagFile(String bagID) throws Exception {
    String pathString = DigestUtils.sha1Hex(bagID);
    // Two level hash-based distribution o files
    String bagPath = Paths.get(Repository.getDataPath(), pathString.substring(0, 2), pathString.substring(2, 4))
            .toString();/*from  ww w  . ja v a2  s . co m*/
    // Create the bag file on disk
    File parent = new File(bagPath);
    if (!parent.exists()) {
        parent.mkdirs();
    }
    // Create known-good filename
    String bagName = getValidName(bagID);
    File bagFile = new File(bagPath, bagName + ".zip");
    log.info("BagPath: " + bagFile.getAbsolutePath());
    // Create an output stream backed by the file
    return bagFile;
}

From source file:org.sead.nds.repository.util.ValidationJob.java

private String generateFileHash(String name, ZipFile zf) {

    ZipArchiveEntry archiveEntry1 = zf.getEntry(name);
    // Error check - add file sizes to compare against supplied stats

    long start = System.currentTimeMillis();
    InputStream inputStream = null;
    String realHash = null;/*  w  ww  . j  a  v a  2 s . c  om*/
    try {
        inputStream = zf.getInputStream(archiveEntry1);
        String hashtype = bagGenerator.getHashtype();
        if (hashtype != null) {
            if (hashtype.equals("SHA1 Hash")) {
                realHash = DigestUtils.sha1Hex(inputStream);
            } else if (hashtype.equals("SHA512 Hash")) {
                realHash = DigestUtils.sha512Hex(inputStream);
            }
        }

    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    log.debug("Retrieve/compute time = " + (System.currentTimeMillis() - start) + " ms");
    // Error check - add file sizes to compare against supplied stats
    bagGenerator.incrementTotalDataSize(archiveEntry1.getSize());
    return realHash;
}

From source file:org.sead.repositories.reference.RefRepository.java

public static String getDataPathTo(String id) {
    String pathString = DigestUtils.sha1Hex(id);
    String path = Repository.getDataPath();
    // Two level hash-based distribution o files
    path = Paths.get(path, pathString.substring(0, 2), pathString.substring(2, 4)).toString();
    log.debug("Path:" + path);
    return path;/*from  w  w  w .j  av a2s.  co  m*/
}

From source file:org.skfiy.typhon.rnsd.service.impl.RechargingServiceImpl.java

@Transactional
@Override//from   ww  w .j a v a  2s.  co m
public void giveZucks(JSONObject json) throws TradeValidatedException {
    Zucks zucks = new Zucks();
    zucks.setOs(OS.valueOf(json.getString("os")));
    zucks.setZid(json.getString("id"));
    zucks.setPoint(json.getIntValue("point"));
    zucks.setUid(json.getString("userId"));

    // validate
    String sha1 = DigestUtils
            .sha1Hex(zucksKeys.get(zucks.getOs()) + zucks.getUid() + zucks.getPoint() + zucks.getZid());
    if (!json.getString("verify").equals(sha1)) {
        throw new TradeValidatedException(sha1, "verify failed");
    }

    rechargingRepository.save(zucks);

    Region region = regionService.loadAll().get(0);

    CloseableHttpClient hc = HC_BUILDER.build();
    HttpHost httpHost = new HttpHost(region.getIp(), region.getJmxPort());

    StringBuilder query = new StringBuilder();
    query.append(
            "/InvokeAction//org.skfiy.typhon.spi%3Aname%3DGMConsoleProvider%2Ctype%3Dorg.skfiy.typhon.spi.GMConsoleProvider/action=pushItem?action=pushItem");
    query.append("&uid%2Bjava.lang.String=").append(zucks.getUid());
    query.append("&iid%2Bjava.lang.String=").append("w036");
    query.append("&count%2Bjava.lang.String=").append(zucks.getPoint());

    HttpGet httpGet = new HttpGet(query.toString());
    httpGet.addHeader("Authorization", basicAuth);

    CloseableHttpResponse response = null;
    try {
        response = hc.execute(httpHost, httpGet);
    } catch (IOException ex) {
        LOG.error("host:port -> {}:{}", region.getIp(), region.getJmxPort(), ex);
        throw new RechargingException("send to game server error error", ex);
    } finally {
        try {
            hc.close();
        } catch (IOException ex) {
        }
    }

    if (response == null || response.getStatusLine().getStatusCode() != HttpStatus.OK.value()) {
        LOG.error("recharging failed. {}", response);
        throw new RechargingException("recharging failed");
    }
}

From source file:org.sonar.db.user.UserDto.java

public static String encryptPassword(String password, String salt) {
    requireNonNull(password, "Password cannot be empty");
    requireNonNull(salt, "Salt cannot be empty");
    return DigestUtils.sha1Hex("--" + salt + "--" + password + "--");
}

From source file:org.sonar.microbenchmark.HashBenchmark.java

@Benchmark
public String commonsCodecSha1() throws Exception {
    return DigestUtils.sha1Hex(bytes);
}

From source file:org.sonar.server.user.UserUpdater.java

private static void setEncryptedPassWord(String password, UserDto userDto) {
    Random random = new SecureRandom();
    byte[] salt = new byte[32];
    random.nextBytes(salt);/*from   w w w  . jav a 2  s.com*/
    String saltHex = DigestUtils.sha1Hex(salt);
    userDto.setSalt(saltHex);
    userDto.setCryptedPassword(encryptPassword(password, saltHex));
}

From source file:org.sonatype.nexus.proxy.GroupingBehaviourTest.java

@Test
public void testMergingVersions() throws Exception {
    String spoofedPath = "/merge-version/maven-metadata.xml";

    File mdmFile = createTempFile("mdm", "tmp");

    try {/*from  w  w  w .  j  av  a 2s.  c o m*/
        Metadata mdm;

        // get metadata from a gidr router, merging should happen
        StorageItem item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath, false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        saveItemToFile((StorageFileItem) item, mdmFile);
        // it should came modified and be different of any existing
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo1" + spoofedPath), mdmFile));
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo2" + spoofedPath), mdmFile));
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo3" + spoofedPath), mdmFile));

        mdm = readMetadata(mdmFile);
        assertEquals("1.2", mdm.getVersioning().getRelease());
        assertEquals(3, mdm.getVersioning().getVersions().size());
        // heh? why?
        // assertEquals( "20020202020202", mdm.getVersioning().getLastUpdated() );

        // get hash from a gidr router, merging should happen and newly calced hash should come down
        item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath + ".md5", false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        String md5hash = contentAsString(item);

        // get hash from a gidr router, merging should happen and newly calced hash should come down
        item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath + ".sha1", false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        String sha1hash = contentAsString(item);

        byte[] buf = FileUtils.readFileToByteArray(mdmFile);
        assertEquals(DigestUtils.md5Hex(buf), md5hash.trim());
        assertEquals(DigestUtils.sha1Hex(buf), sha1hash.trim());
    } finally {
        mdmFile.delete();
    }

}

From source file:org.sonatype.nexus.proxy.GroupingBehaviourTest.java

@Test
public void testMergingPlugins() throws Exception {
    String spoofedPath = "/merge-plugins/maven-metadata.xml";

    File mdmFile = createTempFile("mdm", "tmp");

    try {//from   www  .  j a v  a 2 s .com
        Metadata mdm;

        // get metadata from a gidr router, merging should happen
        StorageItem item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath, false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        saveItemToFile(((StorageFileItem) item), mdmFile);
        // it should came modified and be different of any existing
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo1" + spoofedPath), mdmFile));
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo2" + spoofedPath), mdmFile));
        assertFalse(contentEquals(new File(getBasedir(), "target/test-classes/repo3" + spoofedPath), mdmFile));

        mdm = readMetadata(mdmFile);
        assertTrue(mdm.getPlugins() != null);
        assertEquals(4, mdm.getPlugins().size());
        // heh? why?
        // assertEquals( "20020202020202", mdm.getVersioning().getLastUpdated() );

        // get hash from a gidr router, merging should happen and newly calced hash should come down
        item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath + ".md5", false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        String md5hash = contentAsString(item);

        // get hash from a gidr router, merging should happen and newly calced hash should come down
        item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + spoofedPath + ".sha1", false));
        // it should be a file
        assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));
        // save it
        String sha1hash = contentAsString(item);

        byte[] buf = FileUtils.readFileToByteArray(mdmFile);
        assertEquals(DigestUtils.md5Hex(buf), md5hash.trim());
        assertEquals(DigestUtils.sha1Hex(buf), sha1hash.trim());
    } finally {
        mdmFile.delete();
    }
}

From source file:org.sonatype.nexus.proxy.maven.metadata.GroupMetadataMergeTest.java

@Test
public void testChecksum() throws Exception {
    String mdPath = "/md-merge/checksum/maven-metadata.xml";

    StorageItem item = getRootRouter().retrieveItem(new ResourceStoreRequest("/groups/test" + mdPath, false));
    assertTrue(StorageFileItem.class.isAssignableFrom(item.getClass()));

    File mdFile = createTempFile("metadata", "tmp");
    try {/*from ww w.ja va2  s  .c o  m*/
        saveItemToFile(((StorageFileItem) item), mdFile);

        StorageItem md5Item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + mdPath + ".md5", false));
        StorageItem sha1Item = getRootRouter()
                .retrieveItem(new ResourceStoreRequest("/groups/test" + mdPath + ".sha1", false));

        String md5Hash = contentAsString(md5Item);
        String sha1Hash = contentAsString(sha1Item);

        byte[] buf = FileUtils.readFileToByteArray(mdFile);
        assertEquals(DigestUtils.md5Hex(buf), md5Hash.trim());
        assertEquals(DigestUtils.sha1Hex(buf), sha1Hash.trim());
    } finally {
        mdFile.delete();
    }
}