Search in sources :

Example 1 with Mac

use of javax.crypto.Mac in project Openfire by igniterealtime.

the class N method A.

public static byte[] A(byte[] abyte0, byte[] abyte1, int i, byte[] abyte2) {
    try {
        SecretKeySpec secretkeyspec = new SecretKeySpec(abyte2, "hmacSHA256");
        Mac mac = Mac.getInstance("hmacSHA256");
        mac.init(secretkeyspec);
        mac.update(abyte1, 0, i);
        byte[] abyte3 = mac.doFinal();
        secretkeyspec = new SecretKeySpec(abyte0, "hmacSHA256");
        mac = Mac.getInstance("hmacSHA256");
        mac.init(secretkeyspec);
        return mac.doFinal(abyte3);
    } catch (NoSuchAlgorithmException nosuchalgorithmexception) {
        E.error(nosuchalgorithmexception.getMessage(), nosuchalgorithmexception);
    } catch (InvalidKeyException invalidkeyexception) {
        E.error(invalidkeyexception.getMessage(), invalidkeyexception);
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 2 with Mac

use of javax.crypto.Mac in project OpenAttestation by OpenAttestation.

the class Diagnostic method tryMacWithPassword.

private static void tryMacWithPassword(String algorithmName, String message, String password) {
    try {
        SecretKeySpec key = new SecretKeySpec(password.getBytes(), algorithmName);
        // a string like "HmacSHA256"
        Mac mac = Mac.getInstance(algorithmName, "BC");
        mac.init(key);
        byte[] digest = mac.doFinal(message.getBytes());
        System.out.println("Created " + algorithmName + " digest of length " + digest.length);
    } catch (NoSuchProviderException e) {
        System.err.println("Cannot use provider: BC: " + e.toString());
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
    } catch (InvalidKeyException e) {
        System.err.println("Cannot use key: " + password + ": " + e.toString());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 3 with Mac

use of javax.crypto.Mac in project Signal-Android by WhisperSystems.

the class DecryptingPartInputStream method initializeMac.

private Mac initializeMac(SecretKeySpec key) throws NoSuchAlgorithmException, InvalidKeyException {
    Mac hmac = Mac.getInstance("HmacSHA1");
    hmac.init(key);
    return hmac;
}
Also used : Mac(javax.crypto.Mac)

Example 4 with Mac

use of javax.crypto.Mac in project Openfire by igniterealtime.

the class Crypto method getHmacSha1.

public static Byte[] getHmacSha1(Byte[] key, Byte[] buffer) {
    try {
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(new SecretKeySpec(BitAssistant.bytesFromArray(key), "HmacSHA1"));
        return BitAssistant.bytesToArray(mac.doFinal(BitAssistant.bytesFromArray(buffer)));
    } catch (Exception e) {
        return new Byte[0];
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Example 5 with Mac

use of javax.crypto.Mac in project android_frameworks_base by ResurrectionRemix.

the class MacAuthenticatedInputStreamTest method testString1Authenticate_NullTag_Failure.

public void testString1Authenticate_NullTag_Failure() throws Exception {
    Mac mac = Mac.getInstance("HMAC-SHA1");
    mac.init(HMAC_KEY_1);
    MacAuthenticatedInputStream is = new MacAuthenticatedInputStream(mTestStream1, mac);
    assertTrue(Arrays.equals(TEST_STRING_1, Streams.readFully(is)));
    assertFalse(is.isTagEqual(null));
}
Also used : Mac(javax.crypto.Mac)

Aggregations

Mac (javax.crypto.Mac)475 SecretKeySpec (javax.crypto.spec.SecretKeySpec)318 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)164 InvalidKeyException (java.security.InvalidKeyException)122 SecretKey (javax.crypto.SecretKey)68 IOException (java.io.IOException)44 UnsupportedEncodingException (java.io.UnsupportedEncodingException)32 GeneralSecurityException (java.security.GeneralSecurityException)24 Cipher (javax.crypto.Cipher)21 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 InputStream (java.io.InputStream)13 IvParameterSpec (javax.crypto.spec.IvParameterSpec)13 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)10 ByteBuffer (java.nio.ByteBuffer)9 SecureRandom (java.security.SecureRandom)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)6