Search in sources :

Example 1 with DigestMethodType

use of com.evolveum.prism.xml.ns._public.types_3.DigestMethodType in project midpoint by Evolveum.

the class ProtectorImpl method compareHashedPbkd.

private boolean compareHashedPbkd(HashedDataType hashedDataType, String algorithmName, char[] clearChars) throws EncryptionException {
    DigestMethodType digestMethodType = hashedDataType.getDigestMethod();
    byte[] salt = digestMethodType.getSalt();
    Integer workFactor = digestMethodType.getWorkFactor();
    byte[] digestValue = hashedDataType.getDigestValue();
    int keyLen = digestValue.length * 8;
    SecretKeyFactory secretKeyFactory;
    try {
        secretKeyFactory = SecretKeyFactory.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    PBEKeySpec keySpec = new PBEKeySpec(clearChars, salt, workFactor, keyLen);
    SecretKey key;
    try {
        key = secretKeyFactory.generateSecret(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    byte[] hashBytes = key.getEncoded();
    return Arrays.equals(digestValue, hashBytes);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DigestMethodType(com.evolveum.prism.xml.ns._public.types_3.DigestMethodType) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 2 with DigestMethodType

use of com.evolveum.prism.xml.ns._public.types_3.DigestMethodType in project midpoint by Evolveum.

the class ProtectorImpl method hashPbkd.

private HashedDataType hashPbkd(ProtectedData<String> protectedData, String algorithmUri, String algorithmName) throws EncryptionException {
    char[] clearChars = getClearChars(protectedData);
    byte[] salt = generatePbkdSalt();
    int iterations = getPbkdIterations();
    SecretKeyFactory secretKeyFactory;
    try {
        secretKeyFactory = SecretKeyFactory.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    PBEKeySpec keySpec = new PBEKeySpec(clearChars, salt, iterations, getPbkdKeyLength());
    SecretKey key;
    try {
        key = secretKeyFactory.generateSecret(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    byte[] hashBytes = key.getEncoded();
    HashedDataType hashedDataType = new HashedDataType();
    DigestMethodType digestMethod = new DigestMethodType();
    digestMethod.setAlgorithm(algorithmUri);
    digestMethod.setSalt(salt);
    digestMethod.setWorkFactor(iterations);
    hashedDataType.setDigestMethod(digestMethod);
    hashedDataType.setDigestValue(hashBytes);
    return hashedDataType;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DigestMethodType(com.evolveum.prism.xml.ns._public.types_3.DigestMethodType) SecretKeyFactory(javax.crypto.SecretKeyFactory) HashedDataType(com.evolveum.prism.xml.ns._public.types_3.HashedDataType)

Example 3 with DigestMethodType

use of com.evolveum.prism.xml.ns._public.types_3.DigestMethodType in project midpoint by Evolveum.

the class ProtectorImpl method compareHashed.

private boolean compareHashed(ProtectedStringType hashedPs, char[] clearChars) throws SchemaException, EncryptionException {
    HashedDataType hashedDataType = hashedPs.getHashedDataType();
    DigestMethodType digestMethodType = hashedDataType.getDigestMethod();
    if (digestMethodType == null) {
        throw new SchemaException("No digest type");
    }
    String algorithmUri = digestMethodType.getAlgorithm();
    QName algorithmQName = QNameUtil.uriToQName(algorithmUri);
    String algorithmNamespace = algorithmQName.getNamespaceURI();
    if (algorithmNamespace == null) {
        throw new SchemaException("No algorithm namespace");
    }
    switch(algorithmNamespace) {
        case PrismConstants.NS_CRYPTO_ALGORITHM_PBKD:
            return compareHashedPbkd(hashedDataType, algorithmQName.getLocalPart(), clearChars);
        default:
            throw new SchemaException("Unkown namespace " + algorithmNamespace);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) DigestMethodType(com.evolveum.prism.xml.ns._public.types_3.DigestMethodType) HashedDataType(com.evolveum.prism.xml.ns._public.types_3.HashedDataType)

Aggregations

DigestMethodType (com.evolveum.prism.xml.ns._public.types_3.DigestMethodType)3 HashedDataType (com.evolveum.prism.xml.ns._public.types_3.HashedDataType)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SecretKey (javax.crypto.SecretKey)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 PBEKeySpec (javax.crypto.spec.PBEKeySpec)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 QName (javax.xml.namespace.QName)1