Search in sources :

Example 6 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hadoop by apache.

the class Signer method computeSignature.

/**
   * Returns then signature of a string.
   *
   * @param secret The secret to use
   * @param str string to sign.
   *
   * @return the signature for the string.
   */
protected String computeSignature(byte[] secret, String str) {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(str.getBytes(Charset.forName("UTF-8")));
        md.update(secret);
        byte[] digest = md.digest();
        return new Base64(0).encodeToString(digest);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("It should not happen, " + ex.getMessage(), ex);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 7 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hadoop by apache.

the class JavaKeyStoreProvider method getMetadata.

@Override
public Metadata getMetadata(String name) throws IOException {
    readLock.lock();
    try {
        if (cache.containsKey(name)) {
            return cache.get(name);
        }
        try {
            if (!keyStore.containsAlias(name)) {
                return null;
            }
            Metadata meta = ((KeyMetadata) keyStore.getKey(name, password)).metadata;
            cache.put(name, meta);
            return meta;
        } catch (ClassCastException e) {
            throw new IOException("Can't cast key for " + name + " in keystore " + path + " to a KeyMetadata. Key may have been added using " + " keytool or some other non-Hadoop method.", e);
        } catch (KeyStoreException e) {
            throw new IOException("Can't get metadata for " + name + " from keystore " + path, e);
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Can't get algorithm for " + name + " from keystore " + path, e);
        } catch (UnrecoverableKeyException e) {
            throw new IOException("Can't recover key for " + name + " from keystore " + path, e);
        }
    } finally {
        readLock.unlock();
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 8 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hadoop by apache.

the class TestLoadBalancingKMSClientProvider method testLoadBalancingWithFailure.

@Test
public void testLoadBalancingWithFailure() throws Exception {
    Configuration conf = new Configuration();
    KMSClientProvider p1 = mock(KMSClientProvider.class);
    when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class))).thenReturn(new KMSClientProvider.KMSKeyVersion("p1", "v1", new byte[0]));
    when(p1.getKMSUrl()).thenReturn("p1");
    // This should not be retried
    KMSClientProvider p2 = mock(KMSClientProvider.class);
    when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class))).thenThrow(new NoSuchAlgorithmException("p2"));
    when(p2.getKMSUrl()).thenReturn("p2");
    KMSClientProvider p3 = mock(KMSClientProvider.class);
    when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class))).thenReturn(new KMSClientProvider.KMSKeyVersion("p3", "v3", new byte[0]));
    when(p3.getKMSUrl()).thenReturn("p3");
    // This should be retried
    KMSClientProvider p4 = mock(KMSClientProvider.class);
    when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class))).thenThrow(new IOException("p4"));
    when(p4.getKMSUrl()).thenReturn("p4");
    KeyProvider kp = new LoadBalancingKMSClientProvider(new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf);
    assertEquals("p1", kp.createKey("test4", new Options(conf)).getName());
    // Exceptions other than IOExceptions will not be retried
    try {
        kp.createKey("test1", new Options(conf)).getName();
        fail("Should fail since its not an IOException");
    } catch (Exception e) {
        assertTrue(e instanceof NoSuchAlgorithmException);
    }
    assertEquals("p3", kp.createKey("test2", new Options(conf)).getName());
    // IOException will trigger retry in next provider
    assertEquals("p1", kp.createKey("test3", new Options(conf)).getName());
}
Also used : KeyProvider(org.apache.hadoop.crypto.key.KeyProvider) Options(org.apache.hadoop.crypto.key.KeyProvider.Options) Configuration(org.apache.hadoop.conf.Configuration) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 9 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hadoop by apache.

the class MRAppMaster method initJobCredentialsAndUGI.

// end createJob()
/**
   * Obtain the tokens needed by the job and put them in the UGI
   * @param conf
   */
protected void initJobCredentialsAndUGI(Configuration conf) {
    try {
        this.currentUser = UserGroupInformation.getCurrentUser();
        this.jobCredentials = ((JobConf) conf).getCredentials();
        if (CryptoUtils.isEncryptedSpillEnabled(conf)) {
            int keyLen = conf.getInt(MRJobConfig.MR_ENCRYPTED_INTERMEDIATE_DATA_KEY_SIZE_BITS, MRJobConfig.DEFAULT_MR_ENCRYPTED_INTERMEDIATE_DATA_KEY_SIZE_BITS);
            KeyGenerator keyGen = KeyGenerator.getInstance(INTERMEDIATE_DATA_ENCRYPTION_ALGO);
            keyGen.init(keyLen);
            encryptedSpillKey = keyGen.generateKey().getEncoded();
        } else {
            encryptedSpillKey = new byte[] { 0 };
        }
    } catch (IOException e) {
        throw new YarnRuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new YarnRuntimeException(e);
    }
}
Also used : YarnRuntimeException(org.apache.hadoop.yarn.exceptions.YarnRuntimeException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Example 10 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project hbase by apache.

the class Encryption method hash128.

/**
   * Return the MD5 digest of the concatenation of the supplied arguments.
   */
public static byte[] hash128(byte[]... args) {
    byte[] result = new byte[16];
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        for (byte[] arg : args) {
            md.update(arg);
        }
        md.digest(result, 0, result.length);
        return result;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (DigestException e) {
        throw new RuntimeException(e);
    }
}
Also used : DigestException(java.security.DigestException) 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