Add Hash class
This commit is contained in:
parent
205f8471bf
commit
33471ad1ca
@ -14,6 +14,7 @@ import javax.enterprise.event.Observes;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
import com.covas.Classes.Hash;
|
||||||
import com.covas.Entity.UsersEntity;
|
import com.covas.Entity.UsersEntity;
|
||||||
import com.covas.Enums.Roles;
|
import com.covas.Enums.Roles;
|
||||||
|
|
||||||
@ -34,8 +35,7 @@ public class ApplicationLifeCycle {
|
|||||||
if (schemaCreate){
|
if (schemaCreate){
|
||||||
UsersEntity users = new UsersEntity();
|
UsersEntity users = new UsersEntity();
|
||||||
UsersEntity users2 = new UsersEntity();
|
UsersEntity users2 = new UsersEntity();
|
||||||
|
Hash hash = new Hash();
|
||||||
|
|
||||||
if(users.findByPseudo("Peter") == null){
|
if(users.findByPseudo("Peter") == null){
|
||||||
users.pseudo = "Peter";
|
users.pseudo = "Peter";
|
||||||
users.email = "peter@email.com";
|
users.email = "peter@email.com";
|
||||||
@ -43,7 +43,7 @@ public class ApplicationLifeCycle {
|
|||||||
users.firstName = "Peter";
|
users.firstName = "Peter";
|
||||||
users.birth = LocalDate.of(1993, Month.FEBRUARY, 23);
|
users.birth = LocalDate.of(1993, Month.FEBRUARY, 23);
|
||||||
users.status = true;
|
users.status = true;
|
||||||
users.password = "toto";
|
users.password = hash.encryptSHA512("toto");
|
||||||
users.roles = Roles.User;
|
users.roles = Roles.User;
|
||||||
users.persist();
|
users.persist();
|
||||||
LOGGER.info("Peter test was created");
|
LOGGER.info("Peter test was created");
|
||||||
@ -57,7 +57,7 @@ public class ApplicationLifeCycle {
|
|||||||
users2.firstName = "Peter";
|
users2.firstName = "Peter";
|
||||||
users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23);
|
users2.birth = LocalDate.of(1993, Month.FEBRUARY, 23);
|
||||||
users2.status = true;
|
users2.status = true;
|
||||||
users2.password = "toto";
|
users2.password = hash.encryptSHA512("toto");
|
||||||
users2.roles = Roles.Admin;
|
users2.roles = Roles.Admin;
|
||||||
users2.persist();
|
users2.persist();
|
||||||
LOGGER.info("Robert test was created");
|
LOGGER.info("Robert test was created");
|
||||||
|
41
src/main/java/com/covas/Classes/Hash.java
Normal file
41
src/main/java/com/covas/Classes/Hash.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user