Search in sources :

Example 1 with EncryptionMethod

use of fr.xephi.authme.security.crypts.EncryptionMethod in project AuthMeReloaded by AuthMe.

the class PasswordSecurity method initializeEncryptionMethodWithEvent.

/**
 * Get the encryption method from the given {@link HashAlgorithm} value and emit a
 * {@link PasswordEncryptionEvent}. The encryption method from the event is then returned,
 * which may have been changed by an external listener.
 *
 * @param algorithm  The algorithm to retrieve the encryption method for
 *
 * @return The encryption method
 */
private EncryptionMethod initializeEncryptionMethodWithEvent(HashAlgorithm algorithm) {
    EncryptionMethod method = initializeEncryptionMethod(algorithm);
    PasswordEncryptionEvent event = new PasswordEncryptionEvent(method);
    pluginManager.callEvent(event);
    return event.getMethod();
}
Also used : PasswordEncryptionEvent(fr.xephi.authme.events.PasswordEncryptionEvent) EncryptionMethod(fr.xephi.authme.security.crypts.EncryptionMethod)

Example 2 with EncryptionMethod

use of fr.xephi.authme.security.crypts.EncryptionMethod in project AuthMeReloaded by AuthMe.

the class HashAlgorithmIntegrationTest method shouldBeAbleToInstantiateEncryptionAlgorithms.

@Test
public void shouldBeAbleToInstantiateEncryptionAlgorithms() {
    // given / when / then
    for (HashAlgorithm algorithm : HashAlgorithm.values()) {
        if (!HashAlgorithm.CUSTOM.equals(algorithm) && !HashAlgorithm.PLAINTEXT.equals(algorithm)) {
            if (HashAlgorithm.ARGON2.equals(algorithm) && !Argon2.isLibraryLoaded()) {
                System.out.println("[WARNING] Cannot find argon2 library, skipping integration test");
                continue;
            }
            EncryptionMethod method = injector.createIfHasDependencies(algorithm.getClazz());
            if (method == null) {
                fail("Could not create '" + algorithm.getClazz() + "' - forgot to provide some class?");
            }
            HashedPassword hashedPassword = method.computeHash("pwd", "name");
            assertThat("Salt should not be null if method.hasSeparateSalt(), and vice versa. Method: '" + method + "'", StringUtils.isEmpty(hashedPassword.getSalt()), equalTo(!method.hasSeparateSalt()));
            assertThat("Hash should not be empty for method '" + method + "'", StringUtils.isEmpty(hashedPassword.getHash()), equalTo(false));
        }
    }
}
Also used : EncryptionMethod(fr.xephi.authme.security.crypts.EncryptionMethod) HashedPassword(fr.xephi.authme.security.crypts.HashedPassword) Test(org.junit.Test)

Example 3 with EncryptionMethod

use of fr.xephi.authme.security.crypts.EncryptionMethod in project AuthMeReloaded by AuthMe.

the class EncryptionMethodInfoGatherer method createDescription.

/**
 * Creates a description of the given hash algorithm based on its annotations.
 *
 * @param algorithm the algorithm to describe
 * @return description of the hash algorithm
 */
private static MethodDescription createDescription(HashAlgorithm algorithm) {
    Class<? extends EncryptionMethod> clazz = algorithm.getClazz();
    EncryptionMethod method = createEncryptionMethod(clazz);
    MethodDescription description = new MethodDescription(clazz);
    description.setHashLength(method.computeHash("test", "user").getHash().length());
    description.setHasSeparateSalt(method.hasSeparateSalt());
    Map<Class<?>, Annotation> annotationMap = gatherAnnotations(clazz);
    if (annotationMap.containsKey(HasSalt.class)) {
        setSaltInformation(description, returnTyped(annotationMap, HasSalt.class), method);
    }
    if (annotationMap.containsKey(Recommendation.class)) {
        description.setUsage(returnTyped(annotationMap, Recommendation.class).value());
    }
    if (annotationMap.containsKey(AsciiRestricted.class)) {
        description.setAsciiRestricted(true);
    }
    return description;
}
Also used : HasSalt(fr.xephi.authme.security.crypts.description.HasSalt) EncryptionMethod(fr.xephi.authme.security.crypts.EncryptionMethod) Annotation(java.lang.annotation.Annotation)

Aggregations

EncryptionMethod (fr.xephi.authme.security.crypts.EncryptionMethod)3 PasswordEncryptionEvent (fr.xephi.authme.events.PasswordEncryptionEvent)1 HashedPassword (fr.xephi.authme.security.crypts.HashedPassword)1 HasSalt (fr.xephi.authme.security.crypts.description.HasSalt)1 Annotation (java.lang.annotation.Annotation)1 Test (org.junit.Test)1