use of java.security.spec.KeySpec in project karaf by apache.
the class PublicKeyLoginModuleTest method testUnknownKeyRSA.
@Test
public void testUnknownKeyRSA() throws Exception {
Properties options = getLoginModuleOptions();
PublickeyLoginModule module = new PublickeyLoginModule();
Subject subject = new Subject();
String knownModulus = "2504227846033126752625313329217708474924890377669312098933267135871562327792150810915433595733" + "979130785790337621243914845149325143098632580183245971502051291613503136182182218708721890923769091345704" + "119963221758691543226829294312457492456071842409242817598014777158790065648435489978774648853589909638928" + "448069481622573966178879417253888452317622624006445863588961367514293886664167742695648199055900918338245" + "701727653606086096756173044470526840851957391900922886984556493506186438991284463663361749451775578708454" + "0181594148839238901052763862484299588887844606103377160953183624788815045644521767391398467190125279745";
// Generate a PublicKey using the known values
BigInteger modulus = new BigInteger(knownModulus);
BigInteger exponent = new BigInteger("65537");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
module.initialize(subject, new NamePubkeyCallbackHandler("rsa", publicKey), null, options);
assertEquals("Precondition", 0, subject.getPrincipals().size());
try {
module.login();
fail("Failure expected on an unknown user");
} catch (FailedLoginException ex) {
// expected
}
}
use of java.security.spec.KeySpec in project karaf by apache.
the class PublicKeyLoginModuleTest method testECLogin.
@Test
public void testECLogin() throws Exception {
Properties options = getLoginModuleOptions();
PublickeyLoginModule module = new PublickeyLoginModule();
Subject subject = new Subject();
String x = "-29742501866672735446035294501787338870744851402037490785638836399245997090445";
String y = "-47637824304079393691947094099226900728731860400898598466261954347951527449659";
KeyFactory keyFactory = KeyFactory.getInstance("EC");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
module.initialize(subject, new NamePubkeyCallbackHandler("ec", publicKey), null, options);
assertEquals("Precondition", 0, subject.getPrincipals().size());
assertTrue(module.login());
assertTrue(module.commit());
assertFalse(subject.getPrincipals().isEmpty());
assertThat("ec", isIn(names(subject.getPrincipals(UserPrincipal.class))));
assertThat("ssh", isIn(names(subject.getPrincipals(RolePrincipal.class))));
assertTrue(module.logout());
assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
use of java.security.spec.KeySpec in project smoke by textbrowser.
the class Cryptography method pbkdf2.
public static byte[] pbkdf2(byte[] salt, char[] password, int iterations, int length) {
if (password == null || salt == null)
return null;
try {
KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return secretKeyFactory.generateSecret(keySpec).getEncoded();
} catch (Exception exception) {
return null;
}
}
use of java.security.spec.KeySpec in project smoke by textbrowser.
the class Cryptography method generateMacKey.
public static SecretKey generateMacKey(byte[] salt, char[] password, int iterations, int keyDerivationFunction) {
if (password == null || salt == null)
return null;
if (// Argon2id
keyDerivationFunction == 0) {
try {
Argon2BytesGenerator generator = new Argon2BytesGenerator();
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id).withVersion(Argon2Parameters.ARGON2_VERSION_13).withIterations(iterations).withMemoryAsKB(HASH_KEY_LENGTH).withParallelism(NUMBER_OF_CORES).withAdditional(Hex.decode("000908070605040302010f0e0d0c0b0a" + "000908070605040302010f0e0d0c0b0a" + "000908070605040302010f0e0d0c0b0a" + "000908070605040302010f0e0d0c0b0a")).withSecret(new String(password).getBytes(StandardCharsets.UTF_8)).withSalt(salt);
byte[] bytes = new byte[HASH_KEY_LENGTH];
generator.init(builder.build());
generator.generateBytes(password, bytes);
return new SecretKeySpec(bytes, HASH_ALGORITHM);
} catch (Exception exception) {
}
} else // PBKDF2
{
// Bits.
int length = 8 * HASH_KEY_LENGTH;
try {
KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);
return secretKeyFactory.generateSecret(keySpec);
} catch (Exception exception) {
}
}
return null;
}
use of java.security.spec.KeySpec in project fdroidclient by f-droid.
the class ZipSigner method readPrivateKey.
/**
* Read a PKCS 8 format private key.
*/
public PrivateKey readPrivateKey(URL privateKeyUrl, String keyPassword) throws IOException, GeneralSecurityException {
DataInputStream input = new DataInputStream(privateKeyUrl.openStream());
try {
byte[] bytes = readContentAsBytes(input);
KeySpec spec = decryptPrivateKey(bytes, keyPassword);
if (spec == null) {
spec = new PKCS8EncodedKeySpec(bytes);
}
try {
return KeyFactory.getInstance("RSA").generatePrivate(spec);
} catch (InvalidKeySpecException ex) {
return KeyFactory.getInstance("DSA").generatePrivate(spec);
}
} finally {
input.close();
}
}
Aggregations