Java Digest digest(String strSource)

Here you can find the source of digest(String strSource)

Description

digest

License

Open Source License

Declaration

public static String digest(String strSource) throws java.security.NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/* //from   w w  w .  j a  v a 2 s . c o  m
 * @(#)StringUtil.java 1.0 2004-10-11
 *
 * Copyright 2005 UFIDA Software Co. Ltd. All rights reserved.
 * UFIDA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.io.File;
import java.io.IOException;

import java.net.URL;
import java.net.URLConnection;
import java.security.NoSuchAlgorithmException;

import java.util.Iterator;

import java.util.Properties;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String digest(String strSource) throws java.security.NoSuchAlgorithmException {
        StringBuffer digest = new StringBuffer();
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA");
        byte[] sourBytes = strSource.getBytes();
        byte[] digestBytes = md.digest(sourBytes);
        if (digestBytes != null) {
            for (int i = 0, n = digestBytes.length; i < n; i++) {
                digest.append(digestBytes[i]);
            }
        }
        return digest.toString();
    }

    public static String digest(URL[] urls) throws NoSuchAlgorithmException, IOException {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < urls.length; i++) {
            long lastModified = -1;
            if (i > 1)
                buffer.append(";");
            buffer.append(urls[i]);
            if ("file".equals(urls[i].getProtocol())) {
                File file = new File(urls[i].getPath().replace('/', File.separatorChar).replace('|', ':'));
                lastModified = file.lastModified();
            } else {
                URLConnection connection;
                connection = urls[i].openConnection();
                lastModified = connection.getLastModified();
            }

            buffer.append('!').append(lastModified);

        }
        return digest(buffer.toString());
    }

    public static String toString(Object[] arr) {
        return toString(arr, ",");
    }

    @SuppressWarnings("unchecked")
    public static String toString(Object value, String delim) {
        if (value instanceof String) {
            return (String) value;
        } else {
            if (value.getClass().isArray()) {
                return toString((Object[]) value, delim);
            } else if (value instanceof Iterable) {
                return toString((Iterable) value, delim);
            } else {
                return value.toString();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static String toString(Iterable c, String delim, String prefix, String suffix) {
        if (c == null) {
            return "null";
        }
        StringBuffer sb = new StringBuffer();
        Iterator it = c.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i++ > 0) {
                sb.append(delim);
            }
            sb.append(prefix + it.next() + suffix);
        }
        return sb.toString();
    }

    @SuppressWarnings("unchecked")
    public static String toString(Iterable c, String delim) {
        return toString(c, delim, "", "");
    }

    public static String toString(Object value) {
        return toString(value, ",");
    }

    public static String toString(Object[] arr, String delim) {
        if (arr == null) {
            return "null";
        } else {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < arr.length; i++) {
                if (i > 0)
                    sb.append(delim);
                sb.append(arr[i]);
            }
            return sb.toString();
        }
    }

    public static String replace(String s, Properties p) {
        String regex = "\\$\\w+\\W|\\$\\{[^}]+\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(s);

        while (m.find()) {
            String temp = m.group();
            String key = null;
            if (temp.indexOf("{") != -1) {
                key = temp.substring(temp.indexOf("{") + 1, temp.length() - 1);

            } else {
                key = temp.substring(1, temp.length() - 1);
            }
            String value = p.getProperty(key);

            if (value != null) {
                s = s.replace(temp, value);
                m = pattern.matcher(s);
            }
        }

        return s;
    }
}

Related

  1. digest(String raw, String algorithm)
  2. digest(String s, String algorithm)
  3. digest(String source)
  4. digest(String source, byte[] salt)
  5. digest(String source, String algorythm)
  6. digest(String strSrc, String encName)
  7. digest(String text)
  8. digest(String text, String algorithm)
  9. digest(String token)