use of java.security.NoSuchAlgorithmException in project hive by apache.
the class GenericUDFAesBase method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
checkArgsSize(arguments, 2, 2);
checkArgPrimitive(arguments, 0);
checkArgPrimitive(arguments, 1);
// the function should support both string and binary input types
if (canParam0BeStr()) {
checkArgGroups(arguments, 0, inputTypes, STRING_GROUP, BINARY_GROUP);
} else {
checkArgGroups(arguments, 0, inputTypes, BINARY_GROUP);
}
checkArgGroups(arguments, 1, inputTypes, STRING_GROUP, BINARY_GROUP);
if (isStr0 = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputTypes[0]) == STRING_GROUP) {
obtainStringConverter(arguments, 0, inputTypes, converters);
} else {
GenericUDFParamUtils.obtainBinaryConverter(arguments, 0, inputTypes, converters);
}
isKeyConstant = arguments[1] instanceof ConstantObjectInspector;
byte[] key = null;
int keyLength = 0;
if (isStr1 = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputTypes[1]) == STRING_GROUP) {
if (isKeyConstant) {
String keyStr = getConstantStringValue(arguments, 1);
if (keyStr != null) {
key = keyStr.getBytes();
keyLength = key.length;
}
} else {
obtainStringConverter(arguments, 1, inputTypes, converters);
}
} else {
if (isKeyConstant) {
BytesWritable keyWr = GenericUDFParamUtils.getConstantBytesValue(arguments, 1);
if (keyWr != null) {
key = keyWr.getBytes();
keyLength = keyWr.getLength();
}
} else {
GenericUDFParamUtils.obtainBinaryConverter(arguments, 1, inputTypes, converters);
}
}
if (key != null) {
secretKey = getSecretKey(key, keyLength);
}
try {
cipher = Cipher.getInstance("AES");
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
return outputOI;
}
use of java.security.NoSuchAlgorithmException in project tomcat by apache.
the class ConcurrentMessageDigest method digest.
public static byte[] digest(String algorithm, int rounds, byte[]... input) {
Queue<MessageDigest> queue = queues.get(algorithm);
if (queue == null) {
throw new IllegalStateException("Must call init() first");
}
MessageDigest md = queue.poll();
if (md == null) {
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
// first.
throw new IllegalStateException("Must call init() first");
}
}
// Round 1
for (byte[] bytes : input) {
md.update(bytes);
}
byte[] result = md.digest();
// Subsequent rounds
if (rounds > 1) {
for (int i = 1; i < rounds; i++) {
md.update(result);
result = md.digest();
}
}
queue.add(md);
return result;
}
use of java.security.NoSuchAlgorithmException in project tomcat by apache.
the class Util method generateMask.
static byte[] generateMask() {
// SecureRandom is not thread-safe so need to make sure only one thread
// uses it at a time. In theory, the pool could grow to the same size
// as the number of request processing threads. In reality it will be
// a lot smaller.
// Get a SecureRandom from the pool
SecureRandom sr = randoms.poll();
// If one isn't available, generate a new one
if (sr == null) {
try {
sr = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
// Fall back to platform default
sr = new SecureRandom();
}
}
// Generate the mask
byte[] result = new byte[4];
sr.nextBytes(result);
// Put the SecureRandom back in the poll
randoms.add(sr);
return result;
}
use of java.security.NoSuchAlgorithmException in project crate by crate.
the class Blobs method digest.
public static byte[] digest(String content) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.reset();
digest.update(content.getBytes(StandardCharsets.UTF_8));
return digest.digest();
} catch (NoSuchAlgorithmException e) {
return null;
}
}
use of java.security.NoSuchAlgorithmException in project sockjs-netty by cgbystrom.
the class IframePage method generateMd5.
private static String generateMd5(String value) {
String encryptedString = null;
byte[] bytesToBeEncrypted;
try {
// convert string to bytes using a encoding scheme
bytesToBeEncrypted = value.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] theDigest = md.digest(bytesToBeEncrypted);
// convert each byte to a hexadecimal digit
Formatter formatter = new Formatter();
for (byte b : theDigest) {
formatter.format("%02x", b);
}
encryptedString = formatter.toString().toLowerCase();
} catch (UnsupportedEncodingException e) {
// FIXME: Return proper HTTP error
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// FIXME: Return proper HTTP error
e.printStackTrace();
}
return encryptedString;
}
Aggregations