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.magestore.app.pos.api.m1.config.POSConfigDataAccessM1.java

@Override
public boolean checkLicenseKey()
        throws DataAccessException, ConnectionException, ParseException, IOException, ParseException {
    // nu cha load config, cn khi to ch  default
    if (mConfig == null)
        mConfig = new PosConfigDefault();
    ActiveKey activeKey = new PosActiveKey();
    if (mConfig.getValue("webpos/general/active_key") == null)
        return false;

    String baseUrl = getHostUrl(POSDataAccessSessionM1.REST_BASE_URL);
    String extensionName = POSDataAccessSessionM1.REST_EXTENSION_NAME;
    String licensekey = (String) mConfig.getValue("webpos/general/active_key");
    if (licensekey.length() < 68)
        return false;
    CRC32 crc = new CRC32();
    String strExtensionName = licensekey.substring(0, 10) + extensionName;
    crc.update(strExtensionName.getBytes());
    int strDataCrc32 = (int) crc.getValue();
    int crc32Pos = (strDataCrc32 & 0x7FFFFFFF % 51) + 10;
    int md5Length = 32;
    String md5String = licensekey.substring(crc32Pos, (crc32Pos + md5Length));
    int md5StringLength = md5String.length();
    String key = licensekey.substring(0, crc32Pos)
            + licensekey.substring((crc32Pos + md5StringLength + 3), licensekey.length());
    try {/*from   w  ww .  j  av a  2  s  .  c  om*/
        while ((key.length() % 4) != 0) {
            key += "=";
        }
        String licenseString = decryptRSAToString(key, POSDataAccessSessionM1.REST_PUBLIC_KEY);
        if (StringUtil.isNullOrEmpty(licenseString))
            return false;

        String strlicenseString = licenseString.substring(0, 3);
        String strlicensekey = licensekey.substring((crc32Pos + md5StringLength),
                (crc32Pos + md5StringLength + 3));
        if (!strlicenseString.equals(strlicensekey))
            return false;

        String type = licenseString.substring(0, 1);
        String strexpiredTime = licenseString.substring(1, 3);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        long extensionHash = -1;
        try {
            extensionHash = Long.parseLong(licenseString.substring(3, 13));
        } catch (Exception e) {
        }
        CRC32 crcExtensionName = new CRC32();
        crcExtensionName.update(extensionName.getBytes());
        long crc32ExtensionName = crcExtensionName.getValue();
        if (extensionHash != crc32ExtensionName)
            return false;

        String licenseDomain = licenseString.substring(17, licenseString.length()).replaceAll(" ", "");
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception e) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;

        String strDate = licenseString.substring(11, 15);
        int resultDate = Integer.parseInt(String.valueOf(strDate), 16);
        String DATE_FORMAT = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String createdDate = sdf.format(new Date(resultDate * 24 * 3600 * 1000L));
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;

        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setCreatedDate(createdDate);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    } catch (Exception e) {
        String licenseDomain = "";
        if (baseUrl.contains("https://")) {
            baseUrl = baseUrl.replace("https://", "");
        } else if (baseUrl.contains("http://")) {
            baseUrl = baseUrl.replace("http://", "");
        }
        if (baseUrl.length() > 36) {
            licenseDomain = baseUrl.substring(0, 36);
        } else {
            licenseDomain = baseUrl;
        }
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception ex) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;
        String type = licensekey.substring(crc32Pos + md5StringLength, crc32Pos + md5StringLength + 1);
        String strexpiredTime = licensekey.substring(crc32Pos + md5StringLength + 1,
                crc32Pos + md5StringLength + 1 + 2);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;
        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    }
}

From source file:srebrinb.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * /* ww w  . jav a 2s  . c om*/
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = channel.position();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    channel.write(ByteBuffer.wrap(headerBytes));

    final CRC32 crc32 = new CRC32();
    crc32.update(headerBytes);

    ByteBuffer bb = ByteBuffer.allocate(SevenZFile.sevenZSignature.length + 2 /* version */
            + 4 /* start header CRC */
            + 8 /* next header position */
            + 8 /* next header length */
            + 4 /* next header CRC */).order(ByteOrder.LITTLE_ENDIAN);
    // signature header
    channel.position(0);
    bb.put(SevenZFile.sevenZSignature);
    // version
    bb.put((byte) 0).put((byte) 2);

    // placeholder for start header CRC
    bb.putInt(0);

    // start header
    bb.putLong(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE).putLong(0xffffFFFFL & headerBytes.length)
            .putInt((int) crc32.getValue());
    crc32.reset();
    crc32.update(bb.array(), SevenZFile.sevenZSignature.length + 6, 20);
    bb.putInt(SevenZFile.sevenZSignature.length + 2, (int) crc32.getValue());
    bb.flip();
    channel.write(bb);
}

From source file:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given byte array with an instance of.
 *
 * @param bytes/*from w ww .  j a v  a2  s .c o  m*/
 *            The byte array.
 * @return The checksum from the byte array as long. {@link java.util.zip.CRC32} object.
 */
public static long getCheckSumCRC32(byte[] bytes) {
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    long cs = checksum.getValue();
    return cs;
}

From source file:com.magestore.app.pos.api.m2.config.POSConfigDataAccess.java

@Override
public boolean checkLicenseKey()
        throws DataAccessException, ConnectionException, ParseException, IOException, ParseException {
    // nu cha load config, cn khi to ch  default
    if (mConfig == null)
        mConfig = new PosConfigDefault();
    ActiveKey activeKey = new PosActiveKey();
    if (mConfig.getValue("webpos/general/active_key") == null)
        return false;

    String baseUrl = getHostUrl(POSDataAccessSession.REST_BASE_URL);
    String extensionName = POSDataAccessSession.REST_EXTENSION_NAME;
    String licensekey = (String) mConfig.getValue("webpos/general/active_key");
    if (licensekey.length() < 68)
        return false;
    CRC32 crc = new CRC32();
    String strExtensionName = licensekey.substring(0, 10) + extensionName;
    crc.update(strExtensionName.getBytes());
    int strDataCrc32 = (int) crc.getValue();
    int crc32Pos = (strDataCrc32 & 0x7FFFFFFF % 51) + 10;
    int md5Length = 32;
    String md5String = licensekey.substring(crc32Pos, (crc32Pos + md5Length));
    int md5StringLength = md5String.length();
    String key = licensekey.substring(0, crc32Pos)
            + licensekey.substring((crc32Pos + md5StringLength + 3), licensekey.length());
    try {//from w  w w .ja v  a 2  s .  c om
        while ((key.length() % 4) != 0) {
            key += "=";
        }
        String licenseString = decryptRSAToString(key, POSDataAccessSession.REST_PUBLIC_KEY);
        if (StringUtil.isNullOrEmpty(licenseString))
            return false;

        String strlicenseString = licenseString.substring(0, 3);
        String strlicensekey = licensekey.substring((crc32Pos + md5StringLength),
                (crc32Pos + md5StringLength + 3));
        if (!strlicenseString.equals(strlicensekey))
            return false;

        String type = licenseString.substring(0, 1);
        String strexpiredTime = licenseString.substring(1, 3);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        long extensionHash = -1;
        try {
            extensionHash = Long.parseLong(licenseString.substring(3, 13));
        } catch (Exception e) {
        }
        CRC32 crcExtensionName = new CRC32();
        crcExtensionName.update(extensionName.getBytes());
        long crc32ExtensionName = crcExtensionName.getValue();
        if (extensionHash != crc32ExtensionName)
            return false;

        String licenseDomain = licenseString.substring(17, licenseString.length()).replaceAll(" ", "");
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception e) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;

        String strDate = licenseString.substring(11, 15);
        int resultDate = Integer.parseInt(String.valueOf(strDate), 16);
        String DATE_FORMAT = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String createdDate = sdf.format(new Date(resultDate * 24 * 3600 * 1000L));
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;

        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setCreatedDate(createdDate);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    } catch (Exception e) {
        String licenseDomain = "";
        if (baseUrl.contains("https://")) {
            baseUrl = baseUrl.replace("https://", "");
        } else if (baseUrl.contains("http://")) {
            baseUrl = baseUrl.replace("http://", "");
        }
        if (baseUrl.length() > 36) {
            licenseDomain = baseUrl.substring(0, 36);
        } else {
            licenseDomain = baseUrl;
        }
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception ex) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;
        String type = licensekey.substring(crc32Pos + md5StringLength, crc32Pos + md5StringLength + 1);
        String strexpiredTime = licensekey.substring(crc32Pos + md5StringLength + 1,
                crc32Pos + md5StringLength + 1 + 2);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;
        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    }
}

From source file:com.alexholmes.hdfsslurper.WorkerThread.java

private long hdfsFileCRC32(Path path) throws IOException {
    InputStream in = null;/*from  w w  w. ja v  a  2 s. c  o m*/
    CRC32 crc = new CRC32();
    try {
        InputStream is = new BufferedInputStream(path.getFileSystem(config.getConfig()).open(path));
        if (config.getCodec() != null) {
            is = config.getCodec().createInputStream(is);
        }
        in = new CheckedInputStream(is, crc);
        org.apache.commons.io.IOUtils.copy(in, new NullOutputStream());
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(in);
    }
    return crc.getValue();
}

From source file:com.aol.advertising.qiao.util.CommonUtils.java

public static long checkSum(byte[] bytes) {
    CRC32 crc = new CRC32();
    crc.update(bytes);//from  w  w w . jav a  2  s .  c  o m
    return crc.getValue();

}

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

/**
 * returns the CRC32 of the buffer//  w  ww  .  j  ava2  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:srebrinb.compress.sevenzip.SevenZFile.java

private Archive readHeaders(final byte[] password) throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(12 /* signature + 2 bytes version + 4 bytes CRC */)
            .order(ByteOrder.LITTLE_ENDIAN);
    readFully(buf);//from  w ww.j  a  v a2  s . c  o  m
    final byte[] signature = new byte[6];
    buf.get(signature);
    if (!Arrays.equals(signature, sevenZSignature)) {
        throw new IOException("Bad 7z signature");
    }
    // 7zFormat.txt has it wrong - it's first major then minor
    final byte archiveVersionMajor = buf.get();
    final byte archiveVersionMinor = buf.get();
    if (archiveVersionMajor != 0) {
        throw new IOException(
                String.format("Unsupported 7z version (%d,%d)", archiveVersionMajor, archiveVersionMinor));
    }

    final long startHeaderCrc = 0xffffFFFFL & buf.getInt();
    final StartHeader startHeader = readStartHeader(startHeaderCrc);

    final int nextHeaderSizeInt = (int) startHeader.nextHeaderSize;
    if (nextHeaderSizeInt != startHeader.nextHeaderSize) {
        throw new IOException("cannot handle nextHeaderSize " + startHeader.nextHeaderSize);
    }
    channel.position(SIGNATURE_HEADER_SIZE + startHeader.nextHeaderOffset);
    buf = ByteBuffer.allocate(nextHeaderSizeInt).order(ByteOrder.LITTLE_ENDIAN);
    readFully(buf);
    final CRC32 crc = new CRC32();
    crc.update(buf.array());
    if (startHeader.nextHeaderCrc != crc.getValue()) {
        throw new IOException("NextHeader CRC mismatch");
    }

    Archive archive = new Archive();
    int nid = getUnsignedByte(buf);
    if (nid == NID.kEncodedHeader) {
        buf = readEncodedHeader(buf, archive, password);
        // Archive gets rebuilt with the new header
        archive = new Archive();
        nid = getUnsignedByte(buf);
    }
    if (nid == NID.kHeader) {
        readHeader(buf, archive);
    } else {
        throw new IOException("Broken or unsupported archive: no Header");
    }
    return archive;
}

From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java

/**
 * Gets the checksum from the given byte array with an instance of.
 *
 * @param bytes/* w w  w.j ava 2 s  . c o  m*/
 *            The byte array.
 * @return The checksum from the byte array as long. {@link java.util.zip.CRC32} object.
 */
public static long getCheckSumCRC32(final byte[] bytes) {
    final Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    final long cs = checksum.getValue();
    return cs;
}

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

private long createDirectoryFile(FileSystem fileSys, Path name, int repl, long[] fileSizes, long[] blockSizes,
        int[] seeds, long blockSize) throws IOException {
    CRC32 crc = new CRC32();
    assert fileSizes.length == blockSizes.length;
    assert fileSizes.length == seeds.length;
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, blockSize);
    byte[] zeros = new byte[(int) (blockSize)];
    for (int j = 0; j < zeros.length; j++) {
        zeros[j] = 0;//w w  w .j av  a 2  s  .co m
    }
    // fill random data into file
    for (int i = 0; i < fileSizes.length; i++) {
        assert blockSizes[i] <= blockSize;
        byte[] b = new byte[(int) blockSizes[i]];
        long numBlocks = fileSizes[i] / blockSizes[i];
        Random rand = new Random(seeds[i]);
        for (int j = 0; j < numBlocks; j++) {
            rand.nextBytes(b);
            stm.write(b);
            crc.update(b);
            int zeroLen = (int) (blockSize - blockSizes[i]);
            stm.write(zeros, 0, zeroLen);
            crc.update(zeros, 0, zeroLen);
        }
        long lastBlock = fileSizes[i] - numBlocks * blockSizes[i];
        if (lastBlock > 0) {
            b = new byte[(int) lastBlock];
            rand.nextBytes(b);
            stm.write(b);
            crc.update(b);
            if (i + 1 < fileSizes.length) {
                // Not last block of file, write zero
                int zeroLen = (int) (blockSize - lastBlock);
                stm.write(zeros, 0, zeroLen);
                crc.update(zeros, 0, zeroLen);
            }
        }
    }
    stm.close();
    return crc.getValue();
}