Example usage for org.apache.commons.codec.binary Hex Hex

List of usage examples for org.apache.commons.codec.binary Hex Hex

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Hex Hex.

Prototype

public Hex() 

Source Link

Document

Creates a new codec with the default charset name #DEFAULT_CHARSET_NAME

Usage

From source file:edu.ur.file.checksum.Md5ChecksumCalculator.java

/** 
 * Calculate the md5 checksum of the specified file.
 * //from   w w w  .  j  av  a2 s .com
 * @see edu.ur.file.db.ChecksumCalculator#calculate(java.io.File)
 */
public String calculate(File f) {

    String checksum = null;
    try {
        InputStream fis = new FileInputStream(f);

        byte[] buffer = new byte[1024];
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        int numRead;
        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                messageDigest.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
        fis.close();
        byte[] value = messageDigest.digest();

        Hex hex = new Hex();
        checksum = new String(hex.encode(value));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return checksum;

}

From source file:com.arvato.thoroughly.util.security.impl.AES128AttributeEncryptionStrategy.java

public String encrypt(final String s) throws Exception {
    if (StringUtils.isNotEmpty(s)) {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        final SecretKeySpec keySpec = getKeySpec();
        cipher.init(1, keySpec);//from  ww w.j  av  a 2 s .  c  o m

        final byte[] encrypted = cipher.doFinal(s.getBytes());
        final byte[] encoded = new Hex().encode(encrypted);
        return new String(encoded);
    }
    return s;
}

From source file:com.threewks.thundr.util.Encoder.java

public Encoder hex() {
    data = new Hex().encode(data);
    return this;
}

From source file:com.lightboxtechnologies.spectrum.KeyUtilsTest.java

@Test
public void makeOutKeySHA1() throws Exception {
    final Hex hex = new Hex();
    final byte[] hash = hex.decode("64fa477898e268fd30c2bfe272e5a016f5ec31c4".getBytes());
    final byte type = 1;
    final byte[] col = "vassalengine.org/VASSAL 3.1.15".getBytes();

    // It's neat that I have to cast one-byte numeric literals to bytes.
    // Thanks, Java!
    final byte[] expected = { (byte) 0x64, (byte) 0xfa, (byte) 0x47, (byte) 0x78, // SHA1
            (byte) 0x98, (byte) 0xe2, (byte) 0x68, (byte) 0xfd, (byte) 0x30, (byte) 0xc2, (byte) 0xbf,
            (byte) 0xe2, (byte) 0x72, (byte) 0xe5, (byte) 0xa0, (byte) 0x16, (byte) 0xf5, (byte) 0xec,
            (byte) 0x31, (byte) 0xc4, (byte) 0x01, // type
            'v', 'a', 's', 's', 'a', 'l', 'e', 'n', 'g', // column
            'i', 'n', 'e', '.', 'o', 'r', 'g', '/', 'V', 'A', 'S', 'S', 'A', 'L', ' ', '3', '.', '1', '.', '1',
            '5' };

    assertArrayEquals(expected, KeyUtils.makeEntryKey(hash, type, col));
}

From source file:com.threewks.thundr.util.Encoder.java

public Encoder unhex() {
    try {/*from w  w  w. j  a  v  a  2  s.  c  om*/
        data = new Hex().decode(data);
        return this;
    } catch (DecoderException e) {
        throw new BaseException(e, "Failed to convert data from hex: %s", e.getMessage());
    }
}

From source file:com.github.jinahya.codec.HexEncoderTest.java

@Test(enabled = true, invocationCount = 1)
public void testEncodeAgainstCommonsCodecHex() throws EncoderException {

    final byte[] decoded = Tests.decodedBytes();

    final byte[] expected = Tests.uppercase(new Hex().encode(decoded));
    LOGGER.debug("expected: {}", expected);

    final byte[] actual = new HexEncoder().encode(decoded);
    LOGGER.debug("actual: {}", actual);

    Assert.assertEquals(actual, expected);
}

From source file:com.lightboxtechnologies.nsrl.HashLoaderHelperTest.java

protected void writeRowTester(final boolean sha1rowkey) throws Exception {
    final long timestamp = 1234567890;

    final ProdData pd = new ProdData(42, "ACME Roadrunner Decapitator", "1.0", "1", "ACME", "Meep",
            "roadrunner exterminator");

    final MfgData md = new MfgData("ACME", "ACME Corporation");

    final Hex hex = new Hex();
    final byte[] sha1 = (byte[]) hex.decode("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
    final byte[] md5 = (byte[]) hex.decode("deadbeefdeadbeefdeadbeefdeadbeef");
    final byte[] crc32 = (byte[]) hex.decode("deadbeef");

    final byte[] sha1_col = "sha1".getBytes();
    final byte[] md5_col = "md5".getBytes();
    final byte[] crc32_col = "crc32".getBytes();
    final byte[] size_col = "filesize".getBytes();
    final byte[] nsrl_col = "NSRL".getBytes();

    final byte[] family = HBaseTables.HASH_COLFAM_B;

    final byte[] key = sha1rowkey ? sha1 : md5;
    final byte ktype = (byte) (sha1rowkey ? 1 : 0);

    final HashData hd = new HashData(sha1, md5, crc32, "librrkill.so.1", 123456, pd.code, "ACME_OS", "special");

    final ImmutableBytesWritable okey_crc32 = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, crc32_col));
    final KeyValue kv_crc32 = new KeyValue(key, family, crc32_col, timestamp, crc32);

    final ImmutableBytesWritable okey_sha1 = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, sha1_col));
    final KeyValue kv_sha1 = new KeyValue(key, family, sha1_col, timestamp, sha1);

    final ImmutableBytesWritable okey_md5 = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, md5_col));
    final KeyValue kv_md5 = new KeyValue(key, family, md5_col, timestamp, md5);

    final ImmutableBytesWritable okey_size = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, size_col));
    final KeyValue kv_size = new KeyValue(key, family, size_col, timestamp, Bytes.toBytes(hd.size));
    final ImmutableBytesWritable okey_nsrl = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, nsrl_col));
    final KeyValue kv_nsrl = new KeyValue(key, family, nsrl_col, timestamp, nsrl_col);

    final byte[] prod_col = (md.name + '/' + pd.name + ' ' + pd.version).getBytes();
    final ImmutableBytesWritable okey_prod = new ImmutableBytesWritable(
            KeyUtils.makeEntryKey(key, ktype, prod_col));
    final KeyValue kv_prod = new KeyValue(key, family, prod_col, timestamp, prod_col);

    final Map<Integer, List<ProdData>> prod = Collections.singletonMap(pd.code, Collections.singletonList(pd));
    final Map<String, MfgData> mfg = Collections.singletonMap(md.code, md);
    final Map<String, OSData> os = null;

    @SuppressWarnings("unchecked")
    final HashLoaderMapper.Context ctx = context.mock(HashLoaderMapper.Context.class);

    context.checking(new Expectations() {
        {//  w w w.j av  a 2  s .co  m
            oneOf(ctx).write(okey_crc32, kv_crc32);

            if (sha1rowkey) {
                oneOf(ctx).write(okey_md5, kv_md5);
            } else {
                oneOf(ctx).write(okey_sha1, kv_sha1);
            }

            oneOf(ctx).write(okey_size, kv_size);
            oneOf(ctx).write(okey_nsrl, kv_nsrl);
            oneOf(ctx).write(okey_prod, kv_prod);
        }
    });

    final HashLoaderHelper hlh = new HashLoaderHelper(prod, mfg, os, timestamp);
    hlh.writeRow(key, hd, ctx);
}

From source file:com.lightboxtechnologies.ingest.InfoPutter.java

public int run(String[] args) throws DecoderException, IOException {
    if (args.length != 3) {
        System.err.println("Usage: InfoPutter <imageID> <friendly_name> <pathToImageOnHDFS>");
        return 1;
    }//from w  w w.j a v a 2  s .co  m

    final Configuration conf = getConf();

    final String imageID = args[0];
    final String friendlyName = args[1];
    final String imgPath = args[2];

    HTable imgTable = null;

    try {
        imgTable = HBaseTables.summon(conf, HBaseTables.IMAGES_TBL_B, HBaseTables.IMAGES_COLFAM_B);

        // check whether the image ID is in the images table
        final byte[] hash = new Hex().decode(imageID.getBytes());

        final Get get = new Get(hash);
        final Result result = imgTable.get(get);

        if (result.isEmpty()) {
            // row does not exist, add it

            final byte[] friendly_col = "friendly_name".getBytes();
            final byte[] json_col = "json".getBytes();
            final byte[] path_col = "img_path".getBytes();

            final byte[] friendly_b = friendlyName.getBytes();
            final byte[] json_b = IOUtils.toByteArray(System.in);
            final byte[] path_b = imgPath.getBytes();

            final Put put = new Put(hash);
            put.add(HBaseTables.IMAGES_COLFAM_B, friendly_col, friendly_b);
            put.add(HBaseTables.IMAGES_COLFAM_B, json_col, json_b);
            put.add(HBaseTables.IMAGES_COLFAM_B, path_col, path_b);

            imgTable.put(put);

            return 0;
        } else {
            // row exists, fail!
            return 1;
        }
    } finally {
        if (imgTable != null) {
            imgTable.close();
        }
    }
}

From source file:br.eti.fernandoribeiro.rhq.cloudserverpro.SignatureClientFilter.java

@Override
public ClientResponse handle(final ClientRequest request) {
    ClientResponse result = null;//  ww w . ja v  a  2s .  co  m

    try {
        final Map<String, List<Object>> headers = request.getHeaders();

        final List<Object> value = new ArrayList<Object>();

        final DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");

        final String timestamp = formatter.format(date);

        final Hex encoder = new Hex();

        final MessageDigest digester = MessageDigest.getInstance("SHA1");

        value.add(login + ":" + timestamp + ":" + Base64.encodeBase64String(
                encoder.encode(digester.digest((login + contentLength + timestamp + secretKey).getBytes()))));

        headers.put(HeaderNames.API_SIGNATURE, value);

        result = getNext().handle(request);
    } catch (final NoSuchAlgorithmException e) {
    }

    return result;
}

From source file:com.arvato.thoroughly.util.security.impl.AES128AttributeEncryptionStrategy.java

public String decrypt(final String c) throws Exception {
    if (StringUtils.isNotEmpty(c)) {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        final SecretKeySpec keySpec = getKeySpec();

        cipher.init(2, keySpec);/*from w  w  w.  ja v  a2s .c  om*/

        final byte[] decoded = new Hex().decode(c.getBytes());
        final byte[] decrypted = cipher.doFinal(decoded);
        return new String(decrypted);
    }
    return c;
}