Example usage for java.util.zip CRC32 CRC32

List of usage examples for java.util.zip CRC32 CRC32

Introduction

In this page you can find the example usage for java.util.zip CRC32 CRC32.

Prototype

public CRC32() 

Source Link

Document

Creates a new CRC32 object.

Usage

From source file:com.yattatech.dbtc.facade.SystemFacade.java

private long calculateChecksum(String json) {
    final byte[] input = json.getBytes();
    final Checksum checksum = new CRC32();
    checksum.update(input, 0, input.length);
    final long checksumValue = checksum.getValue();
    if (Debug.isDebugable()) {
        Debug.d(TAG, "calculate checksum for json=" + json + "\nvalue=" + checksumValue);
    }//  w  w  w. j  a va 2  s .  com
    return checksumValue;
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

/**
 * Returns the crc32 hash for the input String.
 *
 * @param text a String with the text//from   w w  w .  j  a v a 2 s.co m
 * @return a BigInteger with the hash
 */
@Override
public String hashText(final String text) {
    final CRC32 crc32 = new CRC32();
    crc32.reset();
    crc32.update(text.getBytes());
    return Long.toHexString(crc32.getValue());
}

From source file:com.yattatech.dbtc.facade.SystemFacade.java

private boolean isChecksumValid(long checksumValue, String json) {
    final byte[] input = json.getBytes();
    final Checksum checksum = new CRC32();
    checksum.update(input, 0, input.length);
    final long returned = checksum.getValue();
    if (Debug.isDebugable()) {
        Debug.d(TAG, "validate checksum for json=" + json + "\nexpected=" + checksumValue + "\nretrieve="
                + returned);//from w w  w .  j a va  2s .co m
    }
    return checksumValue == returned;
}

From source file:com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest.java

@Test
public void testLegacyPasswordPlainTextIsplaintextNotSet() throws Exception {
    final File sourceConfigFile = new File("src/test/resources/psw_encryption/legacy_plain_notset.properties");
    final File configFile = File.createTempFile(
            "com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest", "test1");
    filesToDelete.add(configFile);/*from   w  w w.j  av a2 s.c om*/
    configFile.deleteOnExit();
    FileUtils.copyFile(sourceConfigFile, configFile);
    final ConfigurationFile cf = new ConfigurationFile(configFile.getAbsolutePath());
    final List<String> origLines = cf.getLines();

    List<String> updatedLines = null;
    if (cf.isInNeedOfUpdate()) {
        updatedLines = cf.saveWithEncryptedPasswords();
    }

    assertTrue(updatedLines.size() > 0);
    final Iterator<String> updatedLinesIter = updatedLines.iterator();
    for (final String origLine : origLines) {
        if (!updatedLinesIter.hasNext()) {
            fail("Updated file has fewer lines than original");
        }
        String updatedLine = updatedLinesIter.next();

        // make sure obsolete properties didn't sneak in somehow
        assertFalse(updatedLine.matches("^.*\\.password\\.isplaintext=.*$"));

        // If this is a password, verify that it was encoded, and that the
        // isencrypted=true was inserted after it
        if (origLine.startsWith("cc.password=")) {
            assertEquals(
                    "cc.password=,\\(f9b^6ck-Sr-A2!jWeRlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_P",
                    updatedLine);
            updatedLine = updatedLinesIter.next();
            assertEquals("cc.password.isencrypted=true", updatedLine);
        } else if (origLine.startsWith("protex.password=")) {
            assertEquals(
                    "protex.password=DQp'L-+/0Fq0jsi2f'\\\\OlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_P",
                    updatedLine);
            updatedLine = updatedLinesIter.next();
            assertEquals("protex.password.isencrypted=true", updatedLine);
        } else if (origLine.startsWith("connector.0.password=")) {
            assertEquals(
                    "connector.0.password=6'ND2^gdVX/0\\$fYH7TeH04Sh8FAG<\\[lI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_PlI'nKT:u_P",
                    updatedLine);
            updatedLine = updatedLinesIter.next();
            assertEquals("connector.0.password.isencrypted=true", updatedLine);
        } else {
            assertEquals(origLine, updatedLine);
        }
    }

    final File testGeneratedUpdatedFile = File.createTempFile(
            "com.blackducksoftware.tools.commonframework.core.config.ConfigurationFileTest",
            "test1_testGeneratedUpdatedFile");
    filesToDelete.add(testGeneratedUpdatedFile);
    testGeneratedUpdatedFile.deleteOnExit();
    FileUtils.writeLines(testGeneratedUpdatedFile, updatedLines);
    final long csumTestGeneratedFile = FileUtils.checksum(testGeneratedUpdatedFile, new CRC32()).getValue();
    final long csumActualFile = FileUtils.checksum(configFile, new CRC32()).getValue();
    assertEquals(csumTestGeneratedFile, csumActualFile);
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./*w  w  w .  j a  v  a 2s. co m*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * 24 + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * 24);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * 24);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[24];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = ByteBuffer.wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:com.haulmont.cuba.core.app.FoldersServiceBean.java

protected ArchiveEntry newStoredEntry(String name, byte[] data) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(name);
    zipEntry.setSize(data.length);//from  www.  jav a 2  s .c  om
    zipEntry.setCompressedSize(zipEntry.getSize());
    CRC32 crc32 = new CRC32();
    crc32.update(data);
    zipEntry.setCrc(crc32.getValue());
    return zipEntry;
}

From source file:org.apache.hadoop.raid.TestRaidShell.java

private long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);/*w  w w . jav a 2 s.c o m*/
    for (int b = 0; b > 0; b = stm.read()) {
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.apache.jackrabbit.oak.segment.file.TarReader.java

/**
 * Tries to read an existing index from the given tar file. The index is
 * returned if it is found and looks valid (correct checksum, passes
 * sanity checks)./*from  ww  w.  j av  a 2  s .c  om*/
 *
 * @param file tar file
 * @param name name of the tar file, for logging purposes
 * @return tar index, or {@code null} if not found or not valid
 * @throws IOException if the tar file could not be read
 */
private static ByteBuffer loadAndValidateIndex(RandomAccessFile file, String name) throws IOException {
    long length = file.length();
    if (length % BLOCK_SIZE != 0 || length < 6 * BLOCK_SIZE || length > Integer.MAX_VALUE) {
        log.warn("Unexpected size {} of tar file {}", length, name);
        return null; // unexpected file size
    }

    // read the index metadata just before the two final zero blocks
    ByteBuffer meta = ByteBuffer.allocate(16);
    file.seek(length - 2 * BLOCK_SIZE - 16);
    file.readFully(meta.array());
    int crc32 = meta.getInt();
    int count = meta.getInt();
    int bytes = meta.getInt();
    int magic = meta.getInt();

    if (magic != INDEX_MAGIC) {
        return null; // magic byte mismatch
    }

    if (count < 1 || bytes < count * TarEntry.SIZE + 16 || bytes % BLOCK_SIZE != 0) {
        log.warn("Invalid index metadata in tar file {}", name);
        return null; // impossible entry and/or byte counts
    }

    // this involves seeking backwards in the file, which might not
    // perform well, but that's OK since we only do this once per file
    ByteBuffer index = ByteBuffer.allocate(count * TarEntry.SIZE);
    file.seek(length - 2 * BLOCK_SIZE - 16 - count * TarEntry.SIZE);
    file.readFully(index.array());
    index.mark();

    CRC32 checksum = new CRC32();
    long limit = length - 2 * BLOCK_SIZE - bytes - BLOCK_SIZE;
    long lastmsb = Long.MIN_VALUE;
    long lastlsb = Long.MIN_VALUE;
    byte[] entry = new byte[TarEntry.SIZE];
    for (int i = 0; i < count; i++) {
        index.get(entry);
        checksum.update(entry);

        ByteBuffer buffer = wrap(entry);
        long msb = buffer.getLong();
        long lsb = buffer.getLong();
        int offset = buffer.getInt();
        int size = buffer.getInt();

        if (lastmsb > msb || (lastmsb == msb && lastlsb > lsb)) {
            log.warn("Incorrect index ordering in tar file {}", name);
            return null;
        } else if (lastmsb == msb && lastlsb == lsb && i > 0) {
            log.warn("Duplicate index entry in tar file {}", name);
            return null;
        } else if (offset < 0 || offset % BLOCK_SIZE != 0) {
            log.warn("Invalid index entry offset in tar file {}", name);
            return null;
        } else if (size < 1 || offset + size > limit) {
            log.warn("Invalid index entry size in tar file {}", name);
            return null;
        }

        lastmsb = msb;
        lastlsb = lsb;
    }

    if (crc32 != (int) checksum.getValue()) {
        log.warn("Invalid index checksum in tar file {}", name);
        return null; // checksum mismatch
    }

    index.reset();
    return index;
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.DownloadItem.java

public void writeToZip(ZipArchiveOutputStream zipOutput) throws IOException {
    if (contentInputStream == null)
        return;/*from   ww w . j  a v  a 2 s  .c o  m*/
    String[] domainParts = StringUtils.split(uri.getHost(), '.');
    StringBuilder path = new StringBuilder();
    for (int i = domainParts.length - 1; i >= 0; i--) {
        path.append(domainParts[i]);
        path.append('/');
    }
    String[] pathParts = StringUtils.split(uri.getPath(), '/');
    for (int i = 0; i < pathParts.length - 1; i++) {
        if (StringUtils.isEmpty(pathParts[i]))
            continue;
        path.append(pathParts[i]);
        path.append('/');
    }
    if (contentDispositionFilename != null)
        path.append(contentDispositionFilename);
    else {
        String lastPart = pathParts == null || pathParts.length == 0 ? null : pathParts[pathParts.length - 1];
        if (StringUtils.isEmpty(lastPart))
            path.append("index");
        else
            path.append(lastPart);
    }
    if (uri.getPath().endsWith("/"))
        path.append("/_index");
    String query = uri.getQuery();
    String fragment = uri.getFragment();
    if (!StringUtils.isEmpty(query) || !StringUtils.isEmpty(fragment)) {
        CRC32 crc32 = new CRC32();
        if (!StringUtils.isEmpty(query))
            crc32.update(query.getBytes());
        if (!StringUtils.isEmpty(fragment))
            crc32.update(fragment.getBytes());
        path.append('.');
        path.append(crc32.getValue());
    }
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(path.toString());
    zipOutput.putArchiveEntry(zipEntry);
    BufferedInputStream bis = null;
    byte[] buffer = new byte[65536];
    try {
        bis = new BufferedInputStream(contentInputStream);
        int l;
        while ((l = bis.read(buffer)) != -1)
            zipOutput.write(buffer, 0, l);
        zipOutput.closeArchiveEntry();
    } finally {
        IOUtils.close(bis);
    }
}

From source file:org.apache.hadoop.raid.TestBlockCopier.java

private long[] createRandomFile(Path file, int repl, int numBlocks) throws IOException {

    long[] crcs = new long[numBlocks];
    CRC32 crc = new CRC32();
    Random rand = new Random();
    FSDataOutputStream stm = fileSys.create(file, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, BLOCK_SIZE);
    // Write whole blocks.
    byte[] b = new byte[(int) BLOCK_SIZE];
    for (int i = 1; i < numBlocks; i++) {
        rand.nextBytes(b);//  ww w  . j  a v a  2s  .  c o  m
        stm.write(b);

        crc.update(b);
        crcs[i - 1] = crc.getValue();
        crc.reset();
    }
    // Write partial block.
    b = new byte[(int) BLOCK_SIZE / 2 - 1];
    rand.nextBytes(b);
    stm.write(b);
    crc.update(b);
    crcs[crcs.length - 1] = crc.getValue();

    stm.close();
    return crcs;//crc.getValue();
}