Example usage for java.security MessageDigest reset

List of usage examples for java.security MessageDigest reset

Introduction

In this page you can find the example usage for java.security MessageDigest reset.

Prototype

public void reset() 

Source Link

Document

Resets the digest for further use.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String password = "secret";
    String algorithm = "SHA";

    byte[] plainText = password.getBytes();

    MessageDigest md = MessageDigest.getInstance(algorithm);

    md.reset();
    md.update(plainText);//from w  ww.  j  a va 2s.c  om
    byte[] encodedPassword = md.digest();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < encodedPassword.length; i++) {
        if ((encodedPassword[i] & 0xff) < 0x10) {
            sb.append("0");
        }
        sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }
    System.out.println("Plain    : " + password);
    System.out.println("Encrypted: " + sb.toString());
}

From source file:org.jflicks.tv.programdata.sd.json.Manage.java

public static void main(String[] args) throws NoSuchAlgorithmException {

    final String LIST_ALL_LINEUPS = "listAllLineups";
    final String LIST_ADDED_LINEUPS = "listAddedLineups";
    final String ADD_LINEUP = "addLineup";
    final String DELETE_LINEUP = "deleteLineup";
    final String LIST_STATIONS = "listStations";
    final String GUIDE = "guide";
    final String PROGRAM = "program";
    final String AUTOMAP = "automap";

    String user = null;//from ww w.j av a  2  s. c  om
    String password = null;
    //String action = LIST_ALL_LINEUPS;
    //String action = LIST_ADDED_LINEUPS;
    //String action = ADD_LINEUP;
    //String action = DELETE_LINEUP;
    //String action = LIST_STATIONS;
    //String action = GUIDE;
    String action = PROGRAM;
    String country = "USA";
    String zip = "12095";
    String lineup = null;
    String location = null;
    String automapJson = null;
    ArrayList<GuideRequest> glist = new ArrayList<GuideRequest>();
    ArrayList<String> plist = new ArrayList<String>();

    for (int i = 0; i < args.length; i += 2) {

        if (args[i].equalsIgnoreCase("-u")) {

            user = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-p")) {

            password = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-l")) {

            location = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-action")) {

            action = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-country")) {

            country = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-zipcode")) {

            zip = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-lineup")) {

            lineup = args[i + 1];

        } else if (args[i].equalsIgnoreCase("-automapFile")) {

            File jsonData = new File(args[i + 1]);
            if ((jsonData.exists()) && (jsonData.isFile())) {

                try {

                    automapJson = FileUtils.readFileToString(jsonData);

                } catch (IOException ex) {
                }
            }

        } else if (args[i].equalsIgnoreCase("-sid")) {

            String sid = args[i + 1];
            GuideRequest gr = new GuideRequest();
            gr.setStationID(sid);
            glist.add(gr);

        } else if (args[i].equalsIgnoreCase("-pid")) {

            String pid = args[i + 1];
            plist.add(pid);
        }
    }

    if ((user != null) && (password != null)) {

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(password.getBytes());
        String sha = new String(Hex.encodeHex(md.digest()));

        Client c = new Client();
        if (c.doToken(user, sha)) {

            if (c.doStatus()) {

                System.err.println("status ok\n");

                // Now perform the desired action.
                switch (action) {

                case LIST_ALL_LINEUPS:
                    if (c.doHeadend(country, zip)) {

                        HeadendObject[] array = c.getHeadendObjects();
                        if ((array != null) && (array.length > 0)) {

                            for (int i = 0; i < array.length; i++) {

                                Lineup[] lups = array[i].getLineups();
                                if ((lups != null) && (lups.length > 0)) {

                                    for (int j = 0; j < lups.length; j++) {

                                        String lupname = lups[j].getName();
                                        lupname = lupname.replaceAll(" ", "-");
                                        System.err.println("lineup=" + lups[j].getLineup() + " name=" + lupname
                                                + " location=" + array[i].getLocation());
                                    }
                                }
                            }

                            System.err.println("\nUse the name and location to add the lineup.");
                            System.err
                                    .println("Plus use the name and location to remove it later if you want.");
                        }

                    } else {

                        System.err.println("No headends found!");
                    }

                    break;

                case LIST_ADDED_LINEUPS:

                    UserLineup ul = c.getUserLineup();
                    if (ul != null) {

                        Lineup[] lups = ul.getLineups();
                        if ((lups != null) && (lups.length > 0)) {

                            for (int i = 0; i < lups.length; i++) {

                                String lupname = lups[i].getName();
                                lupname = lupname.replaceAll(" ", "-");
                                System.err.println(lups[i].getLineup() + " name=" + lupname + " transport="
                                        + lups[i].getTransport() + " location=" + lups[i].getLocation());
                            }
                        }

                    } else {

                        System.err.println("No user lineups configured.");
                    }

                    break;

                case ADD_LINEUP:

                    if (c.doHeadend(country, zip)) {

                        if (c.doAddLineup(lineup, location)) {

                            LineupResponse lr = c.getLineupResponse();
                            if (lr != null) {

                                System.err.println(lr.getMessage());
                            }

                        } else {

                            System.err.println("Failed to add lineup.");
                        }

                    } else {

                        System.err.println("Failed check country or zip");
                    }

                    break;

                case DELETE_LINEUP:

                    if (c.doHeadend(country, zip)) {

                        if (c.doDeleteLineup(lineup, location)) {

                            LineupResponse lr = c.getLineupResponse();
                            if (lr != null) {

                                System.err.println(lr.getMessage());
                            }

                        } else {

                            System.err.println("Failed to delete lineup.");
                        }

                    } else {

                        System.err.println("Failed check country or zip");
                    }

                    break;

                case LIST_STATIONS:

                    if (c.doHeadend(country, zip)) {

                        Mapping mapping = c.getMapping(lineup);
                        if (mapping != null) {

                            Station[] sarray = mapping.getStations();
                            if ((sarray != null) && (sarray.length > 0)) {

                                StringBuilder sb = new StringBuilder();
                                for (int i = 0; i < sarray.length; i++) {

                                    StationID sid = mapping.getStationID(sarray[i].getStationID());
                                    if (sid != null) {

                                        sb.setLength(0);
                                        sb.append(sid.getStationID());
                                        sb.append("=");
                                        String chan = sid.getChannel();
                                        if (chan == null) {

                                            chan = sid.getAtscMajor() + "." + sid.getAtscMinor();
                                        }
                                        sb.append(chan);
                                        sb.append("|");
                                        sb.append(sarray[i].getName());
                                        System.err.println(sb.toString());
                                    }
                                }

                            } else {

                                System.err.println("Failed to get stations.");
                            }

                        } else {

                            System.err.println("Failed to get station mapping.");
                        }

                    } else {

                        System.err.println("Failed check country or zip");
                    }

                    break;

                case GUIDE:

                    if (glist.size() > 0) {

                        GuideRequest[] garray = glist.toArray(new GuideRequest[glist.size()]);
                        StationSchedule[] scheds = c.getGuide(garray);

                    } else {

                        System.err.println("Need Station IDs to get guide.");
                    }

                    break;

                case PROGRAM:

                    if (plist.size() > 0) {

                        System.err.println(c.getToken());
                        String[] parray = plist.toArray(new String[plist.size()]);
                        Program[] progs = c.getPrograms(parray);
                        System.err.println(progs);

                    } else {

                        System.err.println("Need Program IDs to get them.");
                    }

                    break;

                case AUTOMAP:

                    if (automapJson != null) {
                        System.out.println(c.doAutomap(automapJson));
                    }

                    break;
                }

            } else {

                System.err.println("SD server OFFLINE, try later.");
            }

        } else {

            System.err.println("Failed to get token check user/password");
        }

    } else {

        System.err.println("user or password null.");
    }
}

From source file:Main.java

public static byte[] encrypt(String x) throws Exception {
    java.security.MessageDigest d = null;
    d = java.security.MessageDigest.getInstance("SHA-1");
    d.reset();
    d.update(x.getBytes());/*from  w  ww. j a  va 2s  . c o  m*/
    return d.digest();
}

From source file:Main.java

public static byte[] digest(byte[] input, String algoritmo) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algoritmo);
    md.reset();
    return md.digest(input);
}

From source file:Main.java

public static String hashImgUrl(String imgUrl) throws NoSuchAlgorithmException {
    String imgKey = null;/*from  ww w. j a  v a2  s .c o m*/
    MessageDigest m = MessageDigest.getInstance("MD5");
    m.reset();
    m.update(imgUrl.getBytes());
    byte[] digest = m.digest();
    BigInteger bigInt = new BigInteger(1, digest);
    imgKey = bigInt.toString(16);
    while (imgKey.length() < 32)
        imgKey = "0" + imgKey;
    return imgKey;
}

From source file:Main.java

public static byte[] toMD5(byte[] bytes) throws Exception {
    try {//from www. java  2 s.  c o m
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        return algorithm.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception(e);
    }
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.MD5Checksum.java

/**
 * @param file//from ww  w .  j  a va  2s . c o m
 * @return
 * @throws Exception
 */
public static String getMD5Checksum(final File file) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    InputStream fis = new FileInputStream(file);
    try {
        //ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fis);
        DigestInputStream digestInputStream = new DigestInputStream(fis, md);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = digestInputStream.read()) >= 0) {
            byteArrayOutputStream.write(ch);
        }

        byte[] newInput = byteArrayOutputStream.toByteArray();
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(newInput);
    } finally {
        fis.close();
    }
}

From source file:Main.java

private static String computeHash(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();

    byte[] byteData = digest.digest(input.getBytes("UTF-8"));
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }// ww w . ja v a 2s  . c  o m

    return sb.toString();
}

From source file:Main.java

public static String generateHash(String pText) throws Exception {
    String hashValue;/*from  w  w  w  .j  a  va2s.  com*/
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pText.getBytes("ASCII"));
    hashValue = encodeHex(md.digest());
    return hashValue;
}

From source file:Main.java

public final synchronized static byte[] md5(byte[] buff) throws NoSuchAlgorithmException {
    MessageDigest md5Obj = MessageDigest.getInstance("MD5");
    md5Obj.reset();
    return md5Obj.digest(buff);
}