diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index adac74c..54df434 100644 --- a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -14,6 +14,7 @@ import javax.enterprise.event.Observes; import javax.inject.Inject; import javax.transaction.Transactional; +import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; import com.covas.Enums.Roles; @@ -34,8 +35,7 @@ public class ApplicationLifeCycle { if (schemaCreate){ UsersEntity users = new UsersEntity(); UsersEntity users2 = new UsersEntity(); - - + Hash hash = new Hash(); if(users.findByPseudo("Peter") == null){ users.pseudo = "Peter"; users.email = "peter@email.com"; @@ -43,7 +43,7 @@ public class ApplicationLifeCycle { users.firstName = "Peter"; users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users.status = true; - users.password = "toto"; + users.password = hash.encryptSHA512("toto"); users.roles = Roles.User; users.persist(); LOGGER.info("Peter test was created"); @@ -57,7 +57,7 @@ public class ApplicationLifeCycle { users2.firstName = "Peter"; users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23); users2.status = true; - users2.password = "toto"; + users2.password = hash.encryptSHA512("toto"); users2.roles = Roles.Admin; users2.persist(); LOGGER.info("Robert test was created"); diff --git a/src/main/java/com/covas/Classes/Hash.java b/src/main/java/com/covas/Classes/Hash.java new file mode 100644 index 0000000..b59abb4 --- /dev/null +++ b/src/main/java/com/covas/Classes/Hash.java @@ -0,0 +1,41 @@ +package com.covas.Classes; + +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Hash { + + public static String encryptPassword(String input) + { + try { + // getInstance() method is called with algorithm SHA-512 + MessageDigest md = MessageDigest.getInstance("SHA-512"); + + // digest() method is called + // to calculate message digest of the input string + // returned as array of byte + byte[] messageDigest = md.digest(input.getBytes()); + + // Convert byte array into signum representation + BigInteger no = new BigInteger(1, messageDigest); + + // Convert message digest into hex value + String hashtext = no.toString(16); + + // Add preceding 0s to make it 32 bit + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + + // return the HashText + return hashtext; + } + + // For specifying wrong message digest algorithms + catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + +}