Created
March 2, 2017 06:42
-
-
Save dlwhitehurst/75bba1d854e5c89d01edcf8f0215ebc2 to your computer and use it in GitHub Desktop.
CryptoUtil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) CI Wise Inc. All rights reserved. http://www.ciwise.com | |
* The software in this package is published under the terms of the Apache | |
* version 2.0 license, a copy of which has been included with this distribution | |
* in the LICENSE.txt file. | |
* | |
*/ | |
package com.ciwise.accounting.util; | |
import java.nio.charset.Charset; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.NoSuchProviderException; | |
import java.security.SecureRandom; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.IvParameterSpec; | |
/** | |
* @author <a href="mailto:[email protected]">David L. Whitehurst</a> | |
* | |
*/ | |
public class CryptoUtil | |
{ | |
/** | |
* | |
*/ | |
private static String digits = "0123456789abcdef"; | |
/** | |
* constant UTF-8 set of characters | |
*/ | |
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8"); | |
/** | |
* This method returns a string from expected UTF-8 bytes | |
* @param bytes | |
* @return | |
*/ | |
public static String decodeUTF8(byte[] bytes) { | |
return new String(bytes, UTF8_CHARSET); | |
} | |
/** | |
* This method returns a byte-array | |
* @param string | |
* @return | |
*/ | |
public static byte[] encodeUTF8(String string) { | |
return string.getBytes(UTF8_CHARSET); | |
} | |
/** | |
* Return length many bytes of the passed in byte array as a hex string. | |
* | |
* @param data the bytes to be converted. | |
* @param length the number of bytes in the data block to be converted. | |
* @return a hex representation of length bytes of data. | |
*/ | |
public static String toHex(byte[] data, int length) | |
{ | |
StringBuffer buf = new StringBuffer(); | |
for (int i = 0; i != length; i++) | |
{ | |
int v = data[i] & 0xff; | |
buf.append(digits.charAt(v >> 4)); | |
buf.append(digits.charAt(v & 0xf)); | |
} | |
return buf.toString(); | |
} | |
/** | |
* Return the passed in byte array as a hex string. | |
* | |
* @param data the bytes to be converted. | |
* @return a hex representation of data. | |
*/ | |
public static String toHex(byte[] data) | |
{ | |
return toHex(data, data.length); | |
} | |
public static byte[] hexStringToByteArray(String s) { | |
int len = s.length(); | |
byte[] data = new byte[len / 2]; | |
for (int i = 0; i < len; i += 2) { | |
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) | |
+ Character.digit(s.charAt(i+1), 16)); | |
} | |
return data; | |
} | |
/** | |
* Create a key for use with AES. | |
* | |
* @param bitLength | |
* @param random | |
* @return an AES key. | |
* @throws java.security.NoSuchAlgorithmException | |
* @throws java.security.NoSuchProviderException | |
*/ | |
public static SecretKey createKeyForAES( | |
int bitLength, | |
SecureRandom random) | |
throws NoSuchAlgorithmException, NoSuchProviderException | |
{ | |
KeyGenerator generator = KeyGenerator.getInstance("AES"); //, "BC"); | |
generator.init(256, random); | |
return generator.generateKey(); | |
} | |
/** | |
* Create an IV suitable for using with AES in CTR mode. | |
* <p> | |
* The IV will be composed of 4 bytes of message number, | |
* 4 bytes of random data, and a counter of 8 bytes. | |
* | |
* @param messageNumber the number of the message. | |
* @param random a source of randomness | |
* @return an initialised IvParameterSpec | |
*/ | |
public static IvParameterSpec createCtrIvForAES( | |
int messageNumber, | |
SecureRandom random) | |
{ | |
byte[] ivBytes = new byte[16]; | |
// initially randomize | |
random.nextBytes(ivBytes); | |
// set the message number bytes | |
ivBytes[0] = (byte)(messageNumber >> 24); | |
ivBytes[1] = (byte)(messageNumber >> 16); | |
ivBytes[2] = (byte)(messageNumber >> 8); | |
ivBytes[3] = (byte)(messageNumber >> 0); | |
// set the counter bytes to 1 | |
for (int i = 0; i != 7; i++) | |
{ | |
ivBytes[8 + i] = 0; | |
} | |
ivBytes[15] = 1; | |
return new IvParameterSpec(ivBytes); | |
} | |
/** | |
* Convert a byte array of 8 bit characters into a String. | |
* | |
* @param bytes the array containing the characters | |
* @param length the number of bytes to process | |
* @return a String representation of bytes | |
*/ | |
public static String toString( | |
byte[] bytes, | |
int length) | |
{ | |
char[] chars = new char[length]; | |
for (int i = 0; i != chars.length; i++) | |
{ | |
chars[i] = (char)(bytes[i] & 0xff); | |
} | |
return new String(chars); | |
} | |
/** | |
* Convert a byte array of 8 bit characters into a String. | |
* | |
* @param bytes the array containing the characters | |
* @return a String representation of bytes | |
*/ | |
public static String toString( | |
byte[] bytes) | |
{ | |
return toString(bytes, bytes.length); | |
} | |
/** | |
* Convert the passed in String to a byte array by | |
* taking the bottom 8 bits of each character it contains. | |
* | |
* @param string the string to be converted | |
* @return a byte array representation | |
*/ | |
public static byte[] toByteArray( | |
String string) | |
{ | |
byte[] bytes = new byte[string.length()]; | |
char[] chars = string.toCharArray(); | |
for (int i = 0; i != chars.length; i++) | |
{ | |
bytes[i] = (byte)chars[i]; | |
} | |
return bytes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment