Skip to content
Snippets Groups Projects
Commit d561a978 authored by zhengbo's avatar zhengbo
Browse files

update hash function

parent 78987a01
No related branches found
No related tags found
2 merge requests!27Sprint 1 done,!25Login page done
......@@ -43,7 +43,7 @@ public class UserController {
SHAModel hash = new SHAModel();
try{
UserModel userModel = userService.addUserToDB(newUser.getUsername(), hash.get_SHA_1_SecurePassword(newUser.getPassword()), newUser.getEmail(), newUser.getFirstname(), newUser.getLastname(), newUser.getGender());
UserModel userModel = userService.addUserToDB(newUser.getUsername(), hash.toHexString(hash.getSHA(newUser.getPassword())), newUser.getEmail(), newUser.getFirstname(), newUser.getLastname(), newUser.getGender());
response.setMessage("Saved successfully");
response.setStatus(HttpStatus.OK);
response.setData(userModel);
......
package vt.CS5934.SwitchRoom.hash;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class SHAModel {
String salt = getSalt();
public SHAModel() throws NoSuchAlgorithmException {
}
public String get_SHA_1_SecurePassword(String passwordToHash) {
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(salt.getBytes());
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16)
.substring(1));
}
generatedPassword = sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return generatedPassword;
public byte[] getSHA(String input) throws NoSuchAlgorithmException
{
/* MessageDigest instance for hashing using SHA256 */
MessageDigest md = MessageDigest.getInstance("SHA-256");
/* digest() method called to calculate message digest of an input and return array of byte */
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public String getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
return salt.toString();
public String toHexString(byte[] hash)
{
/* Convert byte array of hash into digest */
BigInteger number = new BigInteger(1, hash);
/* Convert the digest into hex value */
StringBuilder hexString = new StringBuilder(number.toString(16));
/* Pad with leading zeros */
while (hexString.length() < 32)
{
hexString.insert(0, '0');
}
return hexString.toString();
}
}
......@@ -21,11 +21,11 @@ public class UserModel {
@Id
@Column(name="user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
private Integer userId;
@Column(name="username",unique=true)
private String username;
@Column(name="password")
@Column(name="password", length = 1000)
private String password;
@Column(name="email")
......
......@@ -37,16 +37,13 @@ async function Post(path: string, bodyData: any) {
})
.then((response) => {
if (response.ok) {
console.log(typeof response);
return response.json();
}
console.log("wwww");
throw new Error(
"Unable to receive POST request from server with url:" + url
);
})
.catch((reason) => {
console.log("aaaa");
console.log("Error on POST request", reason);
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment