Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laudarch.utils;
- import java.io.IOException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- /**
- * Created by laudarch on 16/12/13.
- */
- public class EncryptionUtils {
- public static String sha1(String input) throws NoSuchAlgorithmException {
- MessageDigest mDigest = MessageDigest.getInstance("SHA1");
- byte[] result = mDigest.digest(input.getBytes());
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < result.length; i++) {
- sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
- }
- return sb.toString();
- }
- public static String sha256(String input) throws NoSuchAlgorithmException {
- MessageDigest mDigest = MessageDigest.getInstance("SHA256");
- byte[] result = mDigest.digest(input.getBytes());
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < result.length; i++) {
- sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
- }
- return sb.toString();
- }
- /**
- * Verifies SHA1 checksum
- * @param data to be verified
- * @param testChecksum the expected checksum
- * @return true if the SHA1 checksum of data matches the given SHA1 checksum; false otherwise.
- * @throws NoSuchAlgorithmException
- * @throws IOException
- */
- public static boolean verifySHA1Checksum(String data, String testChecksum) throws NoSuchAlgorithmException, IOException
- {
- String dataHash = EncryptionUtils.sha1(data);
- return dataHash.equals(testChecksum);
- }
- /**
- * Verifies SHA256 checksum
- * @param data to be verified
- * @param testChecksum the expected checksum
- * @return true if the SHA256 checksum of the data matches the given SHA256 checksum; false otherwise.
- * @throws NoSuchAlgorithmException
- * @throws IOException
- */
- public static boolean verifyChecksum(String data, String testChecksum) throws NoSuchAlgorithmException, IOException
- {
- String dataHash = sha256(data);
- return dataHash.equals(testChecksum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement