This function computes the SHA256 hash of input string @param text input text whose SHA256 hash has to be computed @param length length of the text to be returned @return returns SHA256 hash of input text @throws NoSuchAlgorithmException @throws UnsupportedEncodingException
(String text, int length)
| 179 | * @throws UnsupportedEncodingException |
| 180 | */ |
| 181 | public static String SHA256 (String text, int length) throws NoSuchAlgorithmException, UnsupportedEncodingException { |
| 182 | |
| 183 | String resultStr; |
| 184 | MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 185 | |
| 186 | md.update(text.getBytes("UTF-8")); |
| 187 | byte[] digest = md.digest(); |
| 188 | |
| 189 | StringBuffer result = new StringBuffer(); |
| 190 | for (byte b : digest) { |
| 191 | result.append(String.format("%02x", b)); //convert to hex |
| 192 | } |
| 193 | //return result.toString(); |
| 194 | |
| 195 | if(length > result.toString().length()) |
| 196 | { |
| 197 | resultStr = result.toString(); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | resultStr = result.toString().substring(0, length); |
| 202 | } |
| 203 | |
| 204 | return resultStr; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | /*** |
| 209 | * This function encrypts the plain text to cipher text using the key |
nothing calls this directly
no outgoing calls
no test coverage detected