use of javax.crypto.SecretKey in project Talon-for-Twitter by klinker24.
the class TwitterDMPicHelper method computeSignature.
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKey secretKey = null;
byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = baseString.getBytes();
return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}
use of javax.crypto.SecretKey in project Talon-for-Twitter by klinker24.
the class TwitterMultipleImageHelper method computeSignature.
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
SecretKey secretKey = null;
byte[] keyBytes = keyString.getBytes();
secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKey);
byte[] text = baseString.getBytes();
return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}
use of javax.crypto.SecretKey in project android-pbe by nelenkov.
the class Crypto method deriveKeyPkcs12.
public static SecretKey deriveKeyPkcs12(byte[] salt, String password) {
try {
long start = System.currentTimeMillis();
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PKCS12_DERIVATION_ALGORITHM);
SecretKey result = keyFactory.generateSecret(keySpec);
long elapsed = System.currentTimeMillis() - start;
Log.d(TAG, String.format("PKCS#12 key derivation took %d [ms].", elapsed));
return result;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.SecretKey in project android-pbe by nelenkov.
the class Crypto method deriveKeyPad.
// Illustration code only: don't use in production!
public static SecretKey deriveKeyPad(String password) {
try {
long start = System.currentTimeMillis();
byte[] keyBytes = new byte[KEY_LENGTH / 8];
// explicitly fill with zeros
Arrays.fill(keyBytes, (byte) 0x0);
// if password is shorter then key length, it will be zero-padded
// to key length
byte[] passwordBytes = password.getBytes("UTF-8");
int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
SecretKey result = new SecretKeySpec(keyBytes, "AES");
long elapsed = System.currentTimeMillis() - start;
Log.d(TAG, String.format("Padding key derivation took %d [ms].", elapsed));
return result;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
use of javax.crypto.SecretKey in project android-pbe by nelenkov.
the class Crypto method deriveKeyPbkdf2.
public static SecretKey deriveKeyPbkdf2(byte[] salt, String password) {
try {
long start = System.currentTimeMillis();
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_DERIVATION_ALGORITHM);
byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
Log.d(TAG, "key bytes: " + toHex(keyBytes));
SecretKey result = new SecretKeySpec(keyBytes, "AES");
long elapsed = System.currentTimeMillis() - start;
Log.d(TAG, String.format("PBKDF2 key derivation took %d [ms].", elapsed));
return result;
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
Aggregations