Java Digest digest(String source, String algorythm)

Here you can find the source of digest(String source, String algorythm)

Description

digest

License

Open Source License

Declaration

public static String digest(String source, String algorythm) 

Method Source Code


//package com.java2s;
/*/*w w w.java 2 s  . c  o m*/
 * utils-java8
 * Copyright (C) 2014 Raffaele Ragni
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String digest(String source, String algorythm) {
        if (source == null || source.length() == 0) {
            return source;
        }

        try {
            StringBuilder result = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorythm);
            byte[] bytes = md.digest(source.getBytes("UTF-8"));
            for (byte b : bytes) {
                String bb = "0" + Integer.toHexString(b);
                result.append(bb.substring(bb.length() - 2));
            }
            return result.toString();
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            return null;
        }
    }
}

Related

  1. digest(String provider, File file, int radix)
  2. digest(String raw, String algorithm)
  3. digest(String s, String algorithm)
  4. digest(String source)
  5. digest(String source, byte[] salt)
  6. digest(String strSource)
  7. digest(String strSrc, String encName)
  8. digest(String text)
  9. digest(String text, String algorithm)