Example usage for java.util.zip CRC32 getValue

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

Introduction

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

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//  w w w  . j  a  v  a  2 s  .  co m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:edu.hm.cs.fs.scriptinat0r7.model.ScriptDocument.java

/**
 * Computes the hash value of a script document.
 * @return the 32 bit hash value as a long.
 *///w w w  .  j av a2 s . c om
public long computeHashvalue() {
    final CRC32 crc = new CRC32();
    crc.update(getFile());
    return crc.getValue();
}

From source file:org.apache.hadoop.hdfs.TestLookasideCache.java

/**
 * returns the CRC32 of the buffer// www.  j a  v a 2 s . c o  m
 */
private long bufferCRC(byte[] buf) {
    CRC32 crc = new CRC32();
    crc.update(buf, 0, buf.length);
    return crc.getValue();
}

From source file:org.kepler.objectmanager.cache.DataCacheManager.java

/**
 * Return a temporary local LSID based on a string.
 * @param magicstring//from w w w. java 2  s  . c o m
 *     * @throws Exception
 */
private KeplerLSID getDataLSID(String magicstring) throws Exception {
    CRC32 c = new CRC32();
    c.update(magicstring.getBytes());
    String hexValue = Long.toHexString(c.getValue());
    KeplerLSID lsid = new KeplerLSID("localdata", hexValue, 0L, 0L);
    return lsid;
}

From source file:com.jivesoftware.os.amza.service.storage.WALStorage.java

private static long[] buildEndOfMergeMarker(long deltaWALId, long highestTxId, long oldestTimestamp,
        long oldestVersion, long oldestTombstonedTimestamp, long oldestTombstonedVersion, long keyCount,
        long clobberCount, long fpOfLastLeap, long updatesSinceLeap, long[] stripedKeyHighwaterTimestamps,
        int offset) {
    final long[] marker = new long[EOM_HIGHWATER_STRIPES_OFFSET + numKeyHighwaterStripes];
    marker[EOM_VERSION_INDEX] = 1; // version
    marker[EOM_CHECKSUM_INDEX] = 0; // placeholder checksum
    marker[EOM_DELTA_WAL_ID_INDEX] = deltaWALId;
    marker[EOM_HIGHEST_TX_ID_INDEX] = highestTxId;
    marker[EOM_OLDEST_TIMESTAMP_INDEX] = oldestTimestamp;
    marker[EOM_OLDEST_VERSION_INDEX] = oldestVersion;
    marker[EOM_OLDEST_TOMBSTONED_TIMESTAMP_INDEX] = oldestTombstonedTimestamp;
    marker[EOM_OLDEST_TOMBSTONED_VERSION_INDEX] = oldestTombstonedVersion;
    marker[EOM_KEY_COUNT_INDEX] = keyCount;
    marker[EOM_CLOBBER_COUNT_INDEX] = clobberCount;

    marker[EOM_FP_OF_LAST_LEAP_INDEX] = fpOfLastLeap;
    marker[EOM_UPDATES_SINCE_LAST_LEAP_INDEX] = updatesSinceLeap;

    System.arraycopy(stripedKeyHighwaterTimestamps, offset, marker, EOM_HIGHWATER_STRIPES_OFFSET,
            numKeyHighwaterStripes);/* w  w  w.  j a  va2s .  com*/

    CRC32 crC32 = new CRC32();
    byte[] hintsAsBytes = UIO.longsBytes(marker);
    crC32.update(hintsAsBytes, 16, hintsAsBytes.length - 16); // 16 skips the version and checksum
    marker[EOM_CHECKSUM_INDEX] = crC32.getValue();
    return marker;
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//from w w  w. j  a v a 2  s. c  o m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//w w  w  .  j av a  2s  .  c  o m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:org.trancecode.xproc.step.HashStepProcessor.java

@Override
protected void execute(final StepInput input, final StepOutput output) {
    final XdmNode sourceDocument = input.readNode(XProcPorts.SOURCE);
    final String value = input.getOptionValue(XProcOptions.VALUE);
    assert value != null;
    LOG.trace("value = {}", value);
    final String algorithm = input.getOptionValue(XProcOptions.ALGORITHM);
    assert algorithm != null;
    LOG.trace("algorithm = {}", algorithm);
    final String match = input.getOptionValue(XProcOptions.MATCH);
    assert match != null;
    LOG.trace("match = {}", match);
    final String version = input.getOptionValue(XProcOptions.VERSION);
    LOG.trace("version = {}", version);

    final String hashValue;
    if (StringUtils.equalsIgnoreCase("crc", algorithm)) {
        if ("32".equals(version) || version == null) {
            final CRC32 crc32 = new CRC32();
            crc32.update(value.getBytes());
            hashValue = Long.toHexString(crc32.getValue());
        } else {//from  w  w w .ja v  a 2s.c om
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else if (StringUtils.equalsIgnoreCase("md", algorithm)) {
        if (version == null || "5".equals(version)) {
            hashValue = DigestUtils.md5Hex(value);
        } else {
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else if (StringUtils.equalsIgnoreCase("sha", algorithm)) {
        if (version == null || "1".equals(version)) {
            hashValue = DigestUtils.shaHex(value);
        } else if ("256".equals(version)) {
            hashValue = DigestUtils.sha256Hex(value);
        } else if ("384".equals(version)) {
            hashValue = DigestUtils.sha384Hex(value);
        } else if ("512".equals(version)) {
            hashValue = DigestUtils.sha512Hex(value);
        } else {
            throw XProcExceptions.xc0036(input.getLocation());
        }
    } else {
        throw XProcExceptions.xc0036(input.getLocation());
    }

    final SaxonProcessorDelegate hashDelegate = new AbstractSaxonProcessorDelegate() {
        @Override
        public boolean startDocument(final XdmNode node, final SaxonBuilder builder) {
            return true;
        }

        @Override
        public void endDocument(final XdmNode node, final SaxonBuilder builder) {
        }

        @Override
        public EnumSet<NextSteps> startElement(final XdmNode element, final SaxonBuilder builder) {
            builder.text(hashValue);
            return EnumSet.noneOf(NextSteps.class);
        }

        @Override
        public void endElement(final XdmNode node, final SaxonBuilder builder) {
            builder.endElement();
        }

        @Override
        public void attribute(final XdmNode node, final SaxonBuilder builder) {
            builder.attribute(node.getNodeName(), hashValue);
        }

        @Override
        public void comment(final XdmNode node, final SaxonBuilder builder) {
            builder.comment(hashValue);
        }

        @Override
        public void processingInstruction(final XdmNode node, final SaxonBuilder builder) {
            builder.processingInstruction(node.getNodeName().getLocalName(), hashValue);
        }

        @Override
        public void text(final XdmNode node, final SaxonBuilder builder) {
            builder.text(hashValue);
        }
    };

    final SaxonProcessor hashProcessor = new SaxonProcessor(input.getPipelineContext().getProcessor(),
            SaxonProcessorDelegates.forXsltMatchPattern(input.getPipelineContext().getProcessor(), match,
                    input.getStep().getNode(), hashDelegate, new CopyingSaxonProcessorDelegate()));

    final XdmNode result = hashProcessor.apply(sourceDocument);
    output.writeNodes(XProcPorts.RESULT, result);
}

From source file:com.bitbreeds.webrtc.stun.StunMessage.java

public void validate(String pass, byte[] data) {
    StunAttribute fingerprint = this.attributeSet.remove(StunAttributeTypeEnum.FINGERPRINT);
    String finger = new String(fingerprint.getData());
    byte[] fingerprintData = Arrays.copyOfRange(data, 0, data.length - fingerprint.getLength() - 4);
    final CRC32 crc = new CRC32();
    crc.update(fingerprintData);/* w  w  w  . j av  a2s . c o m*/
    String comp = new String(SignalUtil.xor(SignalUtil.fourBytesFromInt((int) crc.getValue()),
            new byte[] { 0x53, 0x54, 0x55, 0x4e }));

    if (!comp.equals(finger)) {
        throw new StunError("Fingerprint bad, computed=" + comp + " sent=" + finger);
    }

    StunAttribute integrity = attributeSet.remove(StunAttributeTypeEnum.MESSAGE_INTEGRITY);

    byte[] integrityData = Arrays.copyOfRange(data, 0,
            data.length - fingerprint.getLength() - integrity.getLength() - 8);
    byte[] lgt = SignalUtil.twoBytesFromInt(fingerprintData.length - 20);
    integrityData[2] = lgt[0];
    integrityData[3] = lgt[1];

    byte[] mac = SignalUtil.hmacSha1(integrityData, pass.getBytes());

    if (!Arrays.equals(mac, integrity.getData())) {
        throw new StunError("Integrity bad, computed=" + Hex.encodeHexString(mac) + " sent="
                + Hex.encodeHexString(integrity.getData()));
    }
}

From source file:org.pentaho.di.trans.steps.checksum.CheckSum.java

private Long calculCheckSum(Object[] r) throws Exception {
    Long retval;/*from ww w  . java  2s .c om*/
    StringBuffer Buff = new StringBuffer();

    // Loop through fields
    for (int i = 0; i < data.fieldnr; i++) {
        String fieldvalue = getInputRowMeta().getString(r, data.fieldnrs[i]);
        Buff.append(fieldvalue);
    }

    if (meta.getCheckSumType().equals(CheckSumMeta.TYPE_CRC32)) {
        CRC32 crc32 = new CRC32();
        crc32.update(Buff.toString().getBytes());
        retval = new Long(crc32.getValue());
    } else {
        Adler32 adler32 = new Adler32();
        adler32.update(Buff.toString().getBytes());
        retval = new Long(adler32.getValue());
    }

    return retval;
}