Merge "Formatting applied to aaa-authn-federation bundle"
[aaa.git] / aaa-authn-api / src / main / java / org / opendaylight / aaa / api / SHA256Calculator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.api;
9
10 import java.security.MessageDigest;
11 import java.util.concurrent.locks.ReentrantReadWriteLock;
12 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 /*
18  * @Author - Sharon Aicler (saichler@cisco.com)
19  */
20 public class SHA256Calculator {
21
22     private static final Logger LOG = LoggerFactory.getLogger(SHA256Calculator.class);
23
24     private static MessageDigest md = null;
25     private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
26     private static WriteLock writeLock = lock.writeLock();
27
28     public static String generateSALT() {
29         StringBuffer salt = new StringBuffer();
30         for (int i = 0; i < 12; i++) {
31             int random = (int) (Math.random() * 24 + 1);
32             salt.append((char) (65 + random));
33         }
34         return salt.toString();
35     }
36
37     public static String getSHA256(byte data[], String salt) {
38         byte SALT[] = salt.getBytes();
39         byte temp[] = new byte[data.length + SALT.length];
40         System.arraycopy(data, 0, temp, 0, data.length);
41         System.arraycopy(SALT, 0, temp, data.length, SALT.length);
42
43         if (md == null) {
44             try {
45                 writeLock.lock();
46                 if (md == null) {
47                     try {
48                         md = MessageDigest.getInstance("SHA-256");
49                     } catch (Exception err) {
50                         LOG.error("Error calculating SHA-256 for SALT", err);
51                     }
52                 }
53             } finally {
54                 writeLock.unlock();
55             }
56         }
57
58         byte by[] = null;
59
60         try {
61             writeLock.lock();
62             md.update(temp);
63             by = md.digest();
64         } finally {
65             writeLock.unlock();
66         }
67         return removeSpecialCharacters(new String(by));
68     }
69
70     public static String getSHA256(String password, String salt) {
71         return getSHA256(password.getBytes(), salt);
72     }
73
74     public static String removeSpecialCharacters(String str) {
75         StringBuilder buff = new StringBuilder();
76         for (int i = 0; i < str.length(); i++) {
77             if (str.charAt(i) != '\'' && str.charAt(i) != 0) {
78                 buff.append(str.charAt(i));
79             }
80         }
81         return buff.toString();
82     }
83 }