/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
package org.opendaylight.aaa.api;
+import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
-
+import javax.xml.bind.DatatypeConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/*
- * @Author - Sharon Aicler (saichler@cisco.com)
+/**
+ * Calculate SHA256.
+ *
+ * @author Sharon Aicler (saichler@cisco.com)
*/
-public class SHA256Calculator {
+@Deprecated
+public final class SHA256Calculator {
private static final Logger LOG = LoggerFactory.getLogger(SHA256Calculator.class);
private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static WriteLock writeLock = lock.writeLock();
+ private SHA256Calculator() {
+
+ }
+
public static String generateSALT() {
StringBuffer salt = new StringBuffer();
for (int i = 0; i < 12; i++) {
return salt.toString();
}
- public static String getSHA256(byte data[], String salt) {
- byte SALT[] = salt.getBytes();
- byte temp[] = new byte[data.length + SALT.length];
+ public static String getSHA256(byte[] data, String salt) {
+ byte[] saltBytes = salt.getBytes(StandardCharsets.UTF_8);
+ byte[] temp = new byte[data.length + saltBytes.length];
System.arraycopy(data, 0, temp, 0, data.length);
- System.arraycopy(SALT, 0, temp, data.length, SALT.length);
+ System.arraycopy(saltBytes, 0, temp, data.length, saltBytes.length);
if (md == null) {
try {
if (md == null) {
try {
md = MessageDigest.getInstance("SHA-256");
- } catch (Exception err) {
- LOG.error("Error calculating SHA-256 for SALT", err);
+ } catch (NoSuchAlgorithmException e) {
+ LOG.error("Error calculating SHA-256 for SALT", e);
}
}
} finally {
}
}
- byte by[] = null;
+ byte[] by = null;
try {
writeLock.lock();
} finally {
writeLock.unlock();
}
- return removeSpecialCharacters(new String(by));
+ // Make sure the outcome hash does not contain special characters
+ return DatatypeConverter.printBase64Binary(by);
}
public static String getSHA256(String password, String salt) {
- return getSHA256(password.getBytes(), salt);
- }
-
- public static String removeSpecialCharacters(String str) {
- StringBuilder buff = new StringBuilder();
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) != '\'' && str.charAt(i) != 0) {
- buff.append(str.charAt(i));
- }
- }
- return buff.toString();
+ return getSHA256(password.getBytes(StandardCharsets.UTF_8), salt);
}
-}
\ No newline at end of file
+}