Search in sources :

Example 16 with NoSuchAlgorithmException

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;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BytesWritable(org.apache.hadoop.io.BytesWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 17 with NoSuchAlgorithmException

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;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 18 with NoSuchAlgorithmException

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;
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 19 with NoSuchAlgorithmException

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;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 20 with NoSuchAlgorithmException

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;
}
Also used : Formatter(java.util.Formatter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1403 MessageDigest (java.security.MessageDigest)548 IOException (java.io.IOException)328 InvalidKeyException (java.security.InvalidKeyException)242 KeyStoreException (java.security.KeyStoreException)168 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)145 CertificateException (java.security.cert.CertificateException)138 UnsupportedEncodingException (java.io.UnsupportedEncodingException)131 KeyManagementException (java.security.KeyManagementException)105 KeyFactory (java.security.KeyFactory)96 NoSuchProviderException (java.security.NoSuchProviderException)93 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)79 UnrecoverableKeyException (java.security.UnrecoverableKeyException)78 KeyStore (java.security.KeyStore)73 SecureRandom (java.security.SecureRandom)72 SSLContext (javax.net.ssl.SSLContext)72 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)69 BadPaddingException (javax.crypto.BadPaddingException)69 Cipher (javax.crypto.Cipher)69