Snippets
Created by
David Whitehurst
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | /**
* 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:david@ciwise.com">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;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.