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:com.anyi.gp.license.RegisterTools.java

public static String decodeString(String encodedStr) {
    String encodeStr = "$#TGDF*FAA&21we@VGXD532w23413!";
    String tempStr = "";
    try {//from w ww  . ja va  2  s.  co  m
        if (encodedStr == null) {
            encodedStr = "";
        }
        //encodedStr = new String(Hex.decodeHex(encodedStr.toCharArray()));
        Hex hex = new Hex();
        encodedStr = new String(hex.decode(encodedStr.getBytes("GBK")), "GBK");
        int i = 0;
        int j = 0;
        for (i = 0; i < encodedStr.length(); i++) {
            if (j >= encodeStr.length()) {
                j = 0;
            }
            char truePass = (char) ~(encodedStr.charAt(i) ^ ~encodeStr.charAt(j));
            tempStr = tempStr + truePass;
            j++;
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
    return tempStr;
}

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

@Test
public void retrieveImageID() throws Exception {
    final Hex hex = new Hex();
    final byte[] hash = hex.decode("deadbeefdeadbeefdeadbeefdeadbeef".getBytes());
    final byte[] imgID = hex.decode("baadf00dbaadf00dbaadf00dbaadf00d".getBytes());
    final byte[] entryID = FsEntryUtils.makeFsEntryKey(imgID, "/etc/passwd".getBytes(), 17);
    final byte[] rowKey = KeyUtils.makeEntryKey(hash, KeyUtils.MD5, entryID);

    assertArrayEquals(imgID, KeyUtils.getImageID(rowKey));
}

From source file:co.cask.hydrator.plugin.EncoderTest.java

@Test
public void testHEXEncoder() throws Exception {
    String test = "This is a test for testing hex encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(
            new Encoder.Config("a:HEX", OUTPUT.toString()));
    transform.initialize(null);/*  www  .  j a  va  2s  .c om*/

    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4")
            .set("e", "5").build(), emitter);

    Hex hex = new Hex();
    byte[] expected = hex.encode(test.getBytes("UTF-8"));
    byte[] actual = emitter.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
}

From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

public static boolean isValid(String plainText, String HMAC, String key) {
    try {/*from  w  ww . jav  a2 s .  c  o m*/
        System.out.println("HMAC on = " + plainText);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256",
                new BouncyCastleProvider());
        char password[] = key.toCharArray();
        byte salt[] = "salt".getBytes();
        KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256");
        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider());
        mac.init(secret);
        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(plainText.getBytes());
        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        //  Covert array of Hex bytes to a String
        String check = new String(hexBytes, "UTF-8");
        System.out.println("Checking = " + check);
        return check.equals(HMAC);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

@Test
public void isType2() throws Exception {
    final Hex hex = new Hex();
    final byte[] hash = hex.decode("deadbeefdeadbeefdeadbeefdeadbeef".getBytes());
    final byte[] imgID = hex.decode("baadf00dbaadf00dbaadf00dbaadf00d".getBytes());
    final byte[] entryID = FsEntryUtils.makeFsEntryKey(imgID, "/etc/passwd".getBytes(), 17);
    final byte[] rowKey = KeyUtils.makeEntryKey(hash, KeyUtils.MD5, entryID);

    assertEquals(true, KeyUtils.isType2(rowKey));
    assertEquals(false, KeyUtils.isType2(hash));
}

From source file:mp3downloader.ZingSearch.java

private static String hash_hmac(String data, String key) {
    try {/*from   w w  w.j  a v  a  2 s .c  o m*/
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacMD5");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacMD5");
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        //  Covert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.baidu.cc.configuration.service.impl.VersionServiceImpl.java

/**
 * ??.//from   ww  w  .  java 2 s . c om
 * 
 * @param file
 *            
 * @param versionId
 *            the version id
 * @throws IOException
 *             ?
 */
@Override
public void importFromFile(File file, Long versionId) throws IOException {
    byte[] byteArray = FileUtils.readFileToByteArray(file);

    Hex encoder = new Hex();
    try {
        byteArray = encoder.decode(byteArray);
    } catch (DecoderException e) {
        throw new IOException(e.getMessage());
    }
    String json = new String(byteArray, SysUtils.UTF_8);

    // parse from gson
    JsonParser jsonParser = new JsonParser();
    JsonElement je = jsonParser.parse(json);

    if (!je.isJsonArray()) {
        throw new RuntimeException("illegal json string. must be json array.");
    }

    JsonArray jsonArray = je.getAsJsonArray();

    int size = jsonArray.size();
    Version version = new Version();
    List<ConfigGroup> groups = new ArrayList<ConfigGroup>();
    ConfigGroup group;
    List<ConfigItem> items;
    ConfigItem item;

    for (int i = 0; i < size; i++) {
        JsonObject jo = jsonArray.get(i).getAsJsonObject();
        group = gson.fromJson(jo, ConfigGroup.class);

        // get sub configuration item
        JsonArray subItemsJson = jo.get(CONFIG_ITEMS_ELE).getAsJsonArray();

        int subSize = subItemsJson.size();
        items = new ArrayList<ConfigItem>();
        for (int j = 0; j < subSize; j++) {
            item = gson.fromJson(subItemsJson.get(j), ConfigItem.class);
            items.add(item);
        }

        group.setConfigItems(items);
        groups.add(group);
    }

    version.setConfigGroups(groups);
    configCopyService.copyConfigItemsFromVersion(version, versionId);
}

From source file:com.bringcommunications.etherpay.Payment_Processor.java

Payment_Processor(Context context) {
    String app_uri = context.getResources().getString(R.string.app_uri);
    preferences = context.getSharedPreferences(app_uri, MODE_PRIVATE);
    send_message_list = new LinkedList<Payment_Message>();
    balance_message_list = new LinkedList<Payment_Message>();
    client_map = new HashMap<Payment_Processor_Client, Integer>();
    monitor = new Object();
    hex = new Hex();
    gas_price_history = new long[5];
    for (int i = 0; i < gas_price_history.length; ++i)
        gas_price_history[i] = Long.MAX_VALUE;
    mem_avail_kb = Util.get_avail_memory_kb(context);
}

From source file:co.cask.hydrator.plugin.DecoderTest.java

@Test
public void testHexDecoder() throws Exception {
    String test = "This is a test for testing hex decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(
            new Encoder.Config("a:HEX", OUTPUT.toString()));
    encoder.initialize(null);/*from  www .  j a  va  2 s .c o m*/

    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4")
            .set("e", "5").build(), emitterEncoded);

    Hex hex = new Hex();
    byte[] expected = hex.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);

    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(
            new Decoder.Config("a:HEX", OUTPUTSTR.toString()));
    decoder.initialize(null);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
}

From source file:co.cask.hydrator.transforms.CSVParser2.java

/**
 * Decodes the payload. //w w  w. j ava  2  s. co  m
 *  
 * @param body
 * @return
 */
private byte[] decodePayLoad(String body) throws DecoderException {
    if (config.decoder.equalsIgnoreCase("base64")) {
        Base64 codec = new Base64();
        return codec.decode(body);
    } else if (config.decoder.equalsIgnoreCase("base32")) {
        Base32 codec = new Base32();
        return codec.decode(body);
    } else if (config.decoder.equalsIgnoreCase("hex")) {
        Hex codec = new Hex();
        return codec.decode(body.getBytes());
    }
    return new byte[0];
}