Java CRC32 Byte Array createRandomCrc32Hex()

Here you can find the source of createRandomCrc32Hex()

Description

create Random Crc Hex

License

Open Source License

Declaration

public static String createRandomCrc32Hex() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Random;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class Main {
    public static final int RANDOM_STRING_LENGTH = 100;
    public static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String createRandomCrc32Hex() {
        return createCrc32Hex(createRandomString());
    }//from   w ww.  j  ava2s  . co  m

    public static String createCrc32Hex(String value) {
        byte bytes[] = value.getBytes();
        Checksum checksumCreator = new CRC32();
        checksumCreator.update(bytes, 0, bytes.length);
        long checkSumValue = checksumCreator.getValue();
        return fixLength(Long.toHexString(checkSumValue));
    }

    public static String createRandomString() {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder(RANDOM_STRING_LENGTH);
        for (int i = 0; i < RANDOM_STRING_LENGTH; i++) {
            stringBuilder.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
        }
        return stringBuilder.toString();
    }

    private static String fixLength(String hexString) {
        while (hexString.length() != 8) {
            hexString = "0" + hexString;
        }
        return hexString;
    }
}

Related

  1. crc32(byte[] bytes)
  2. crc32(final byte[] bytes)
  3. crc32(String value)
  4. crc32Number(String s)
  5. crcBytes(byte[]... data)
  6. getCRC()
  7. getCrc32(byte[] buf)
  8. getCRC32(String str)
  9. getCrc32asInt(byte[] in)