Add Hash class

This commit is contained in:
Valentin CZERYBA 2022-04-15 21:53:49 +02:00
parent 205f8471bf
commit 33471ad1ca
2 changed files with 45 additions and 4 deletions

View File

@ -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");

View File

@ -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);
}
}
}