diff options
author | Demian <demianalonso@gmail.com> | 2015-06-09 19:51:50 -0300 |
---|---|---|
committer | Demian <demianalonso@gmail.com> | 2015-06-09 19:51:50 -0300 |
commit | 96a2d024658746f50628727f7255c4e19ecdb1b4 (patch) | |
tree | fc9642ed3159dd16ebd1c5448233b251b091921c | |
parent | 62048ed5ec8054c9e0205f3308d0954926746866 (diff) | |
download | trackermap-server-96a2d024658746f50628727f7255c4e19ecdb1b4.tar.gz trackermap-server-96a2d024658746f50628727f7255c4e19ecdb1b4.tar.bz2 trackermap-server-96a2d024658746f50628727f7255c4e19ecdb1b4.zip |
Moved hashing function to a separate helper class
-rw-r--r-- | src/org/traccar/database/DataManager.java | 3 | ||||
-rw-r--r-- | src/org/traccar/helper/Hashing.java | 20 | ||||
-rw-r--r-- | src/org/traccar/model/User.java | 21 |
3 files changed, 24 insertions, 20 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index fdab359f4..9633ada58 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -38,6 +38,7 @@ import javax.sql.DataSource; import org.traccar.Context; import org.traccar.helper.DriverDelegate; +import org.traccar.helper.Hashing; import org.traccar.helper.Log; import org.traccar.http.JsonConverter; import org.traccar.model.Device; @@ -219,7 +220,7 @@ public class DataManager { public User login(String email, String password) throws SQLException { return QueryBuilder.create(dataSource, properties.getProperty("database.loginUser")) .setString("email", email) - .setBytes("password", User.sha256(password)) + .setBytes("password", Hashing.sha256(password)) .executeQuerySingle(new User()); } diff --git a/src/org/traccar/helper/Hashing.java b/src/org/traccar/helper/Hashing.java new file mode 100644 index 000000000..7e7579ede --- /dev/null +++ b/src/org/traccar/helper/Hashing.java @@ -0,0 +1,20 @@ +package org.traccar.helper; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Hashing { + + public static byte[] sha256(String text) { + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(text.getBytes(StandardCharsets.UTF_8)); + + return md.digest(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + +} diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index 9186e66c5..410bc4d74 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -15,9 +15,7 @@ */ package org.traccar.model; -import java.io.UnsupportedEncodingException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; +import org.traccar.helper.Hashing; public class User implements Factory { @@ -40,7 +38,7 @@ public class User implements Factory { private byte[] password; public byte[] getPassword() { return password; } - public void setPassword(String password) { this.password = sha256(password); } + public void setPassword(String password) { this.password = Hashing.sha256(password); } private boolean readonly; @@ -61,19 +59,4 @@ public class User implements Factory { private double longitude; private int zoom; - - - public static byte[] sha256(String text) { - try { - MessageDigest md = MessageDigest.getInstance("SHA-256"); - - md.update(text.getBytes("UTF-8")); - byte[] digest = md.digest(); - return digest; - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } } |