List of usage examples for java.security MessageDigest digest
public byte[] digest()
From source file:core.Hash.java
/** * @param args the command line arguments *//* w w w. ja va 2 s . c o m*/ public static void main(String[] args) { MessageDigest md = null; String password = "password D:"; try { //SHA-512 md = MessageDigest.getInstance("SHA-512"); md.update(password.getBytes()); byte[] mb = md.digest(); System.out.println(Hex.encodeHex(mb)); //SHA-1 md = MessageDigest.getInstance("SHA-1"); md.update(password.getBytes()); mb = md.digest(); System.out.println(Hex.encodeHex(mb)); //MD5 md = MessageDigest.getInstance("MD5"); md.update(password.getBytes()); mb = md.digest(); System.out.println(Hex.encodeHex(mb)); } catch (NoSuchAlgorithmException e) { //Error } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: Masher filename"); return;/* w ww.j av a 2 s . c om*/ } MessageDigest md = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(args[0]); byte[] buffer = new byte[8192]; int length; while ((length = in.read(buffer)) != -1) md.update(buffer, 0, length); byte[] raw = md.digest(); BASE64Encoder encoder = new BASE64Encoder(); String base64 = encoder.encode(raw); System.out.println(base64); }
From source file:Main.java
public static void main(String args[]) throws Exception { InputStream fis = new FileInputStream("a.exe"); byte[] buffer = new byte[1024]; MessageDigest complete = MessageDigest.getInstance("MD5"); int numRead;/* w w w . j ava 2 s. com*/ do { numRead = fis.read(buffer); if (numRead > 0) { complete.update(buffer, 0, numRead); } } while (numRead != -1); fis.close(); byte[] b = complete.digest(); String result = ""; for (int i = 0; i < b.length; i++) { result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); } System.out.println(result); }
From source file:com.lightboxtechnologies.spectrum.Uploader.java
public static void main(String[] args) throws Exception { final Configuration conf = new Configuration(); final String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 1) { System.err.println("Usage: Uploader <dest path>"); System.err.println("Writes data to HDFS path from stdin"); System.exit(2);//from ww w. j av a2s . co m } MessageDigest hasher = FsEntryUtils.getHashInstance("MD5"); DigestInputStream hashedIn = new DigestInputStream(System.in, hasher); FileSystem fs = FileSystem.get(conf); Path path = new Path(otherArgs[0]); FSDataOutputStream outFile = fs.create(path, true); IOUtils.copyLarge(hashedIn, outFile, new byte[1024 * 1024]); System.out.println(new String(Hex.encodeHex(hasher.digest()))); }
From source file:MD5.java
public static void main(String[] args) { //String password = args[0]; String password = "test"; MessageDigest digest = null; try {//from w w w .j ava2 s . co m digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } try { digest.update(password.getBytes("UTF-8")); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } byte[] rawData = digest.digest(); StringBuffer printable = new StringBuffer(); for (int i = 0; i < rawData.length; i++) { printable.append(carr[((rawData[i] & 0xF0) >> 4)]); printable.append(carr[(rawData[i] & 0x0F)]); } String phpbbPassword = printable.toString(); System.out.println("PHPBB : " + phpbbPassword); System.out.println("MVNFORUM : " + getMD5_Base64(password)); System.out.println("PHPBB->MVNFORUM : " + getBase64FromHEX(phpbbPassword)); }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream("test"); MessageDigest md = MessageDigest.getInstance("SHA"); DigestInputStream dis = new DigestInputStream(fis, md); ObjectInputStream ois = new ObjectInputStream(dis); Object o = ois.readObject();/*from www. j av a2s. c o m*/ if (!(o instanceof String)) { System.out.println("Unexpected data in file"); System.exit(-1); } String data = (String) o; System.out.println("Got message " + data); dis.on(false); o = ois.readObject(); if (!(o instanceof byte[])) { System.out.println("Unexpected data in file"); System.exit(-1); } byte origDigest[] = (byte[]) o; System.out.println(MessageDigest.isEqual(md.digest(), origDigest)); }
From source file:net.sf.jsignpdf.InstallCert.java
/** * The main - whole logic of Install Cert Tool. * //from www . jav a2 s. c o m * @param args * @throws Exception */ public static void main(String[] args) { String host; int port; char[] passphrase; System.out.println("InstallCert - Install CA certificate to Java Keystore"); System.out.println("====================================================="); final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { if ((args.length == 1) || (args.length == 2)) { String[] c = args[0].split(":"); host = c[0]; port = (c.length == 1) ? 443 : Integer.parseInt(c[1]); String p = (args.length == 1) ? "changeit" : args[1]; passphrase = p.toCharArray(); } else { String tmpStr; do { System.out.print("Enter hostname or IP address: "); tmpStr = StringUtils.defaultIfEmpty(reader.readLine(), null); } while (tmpStr == null); host = tmpStr; System.out.print("Enter port number [443]: "); tmpStr = StringUtils.defaultIfEmpty(reader.readLine(), null); port = tmpStr == null ? 443 : Integer.parseInt(tmpStr); System.out.print("Enter keystore password [changeit]: "); tmpStr = reader.readLine(); String p = "".equals(tmpStr) ? "changeit" : tmpStr; passphrase = p.toCharArray(); } char SEP = File.separatorChar; final File dir = new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security"); final File file = new File(dir, "cacerts"); System.out.println("Loading KeyStore " + file + "..."); InputStream in = new FileInputStream(file); KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(in, passphrase); in.close(); SSLContext context = SSLContext.getInstance("TLS"); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ks); X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0]; SavingTrustManager tm = new SavingTrustManager(defaultTrustManager); context.init(null, new TrustManager[] { tm }, null); SSLSocketFactory factory = context.getSocketFactory(); System.out.println("Opening connection to " + host + ":" + port + "..."); SSLSocket socket = (SSLSocket) factory.createSocket(host, port); socket.setSoTimeout(10000); try { System.out.println("Starting SSL handshake..."); socket.startHandshake(); socket.close(); System.out.println(); System.out.println("No errors, certificate is already trusted"); } catch (SSLException e) { System.out.println(); System.out.println("Certificate is not yet trusted."); // e.printStackTrace(System.out); } X509Certificate[] chain = tm.chain; if (chain == null) { System.out.println("Could not obtain server certificate chain"); return; } System.out.println(); System.out.println("Server sent " + chain.length + " certificate(s):"); System.out.println(); MessageDigest sha1 = MessageDigest.getInstance("SHA1"); MessageDigest md5 = MessageDigest.getInstance("MD5"); for (int i = 0; i < chain.length; i++) { X509Certificate cert = chain[i]; System.out.println(" " + (i + 1) + " Subject " + cert.getSubjectDN()); System.out.println(" Issuer " + cert.getIssuerDN()); sha1.update(cert.getEncoded()); System.out.println(" sha1 " + toHexString(sha1.digest())); md5.update(cert.getEncoded()); System.out.println(" md5 " + toHexString(md5.digest())); System.out.println(); } System.out.print("Enter certificate to add to trusted keystore or 'q' to quit [1]: "); String line = reader.readLine().trim(); int k = -1; try { k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1; } catch (NumberFormatException e) { } if (k < 0 || k >= chain.length) { System.out.println("KeyStore not changed"); } else { try { System.out.println("Creating keystore backup"); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); final File backupFile = new File(dir, CACERTS_KEYSTORE + "." + dateFormat.format(new java.util.Date())); final FileInputStream fis = new FileInputStream(file); final FileOutputStream fos = new FileOutputStream(backupFile); IOUtils.copy(fis, fos); fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Installing certificate..."); X509Certificate cert = chain[k]; String alias = host + "-" + (k + 1); ks.setCertificateEntry(alias, cert); OutputStream out = new FileOutputStream(file); ks.store(out, passphrase); out.close(); System.out.println(); System.out.println(cert); System.out.println(); System.out.println("Added certificate to keystore '" + file + "' using alias '" + alias + "'"); } } catch (Exception e) { System.out.println(); System.out.println("----------------------------------------------"); System.out.println("Problem occured during installing certificate:"); e.printStackTrace(); System.out.println("----------------------------------------------"); } System.out.println("Press Enter to finish..."); try { reader.readLine(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ch.bfh.evoting.verifier.Verifier.java
/** * This program is a verifier for MobiVote application with HKRS12 protocol * You have to indicate the XML file exported from the application as argument for this program. * You can also indicate the files of different participants. In this case, there will be checked if all files contain the same values. * @param args the file(s) containing the values of the protocol *///w w w . j a v a2 s. c o m public static void main(String[] args) { /* * Reading files given as parameters */ if (args.length < 1) { //Not enough arguments System.out.println( "You must indicate the location of the XML file containing the data to verify! You can also indicate the files of different participants."); return; } else if (args.length > 1) { //Make a digest of all files and compare them List<byte[]> digests = new ArrayList<byte[]>(); for (String s : args) { try { MessageDigest md = MessageDigest.getInstance("SHA-256"); InputStream is = Files.newInputStream(Paths.get(s)); DigestInputStream dis = new DigestInputStream(is, md); @SuppressWarnings("unused") int numBytes = -1; while ((numBytes = dis.read()) != -1) { dis.read(); } byte[] digest = md.digest(); digests.add(digest); } catch (IOException e) { System.out.println("One of the given files does not exists!"); return; } catch (NoSuchAlgorithmException e1) { System.out.println("Unable to compute the digest of the file!"); return; } } System.out.print("Checking the similarity of the files..."); for (byte[] b : digests) { for (byte[] b2 : digests) { if (!Arrays.equals(b, b2)) { System.out.print("\t\t\t\t\t\t\t\t\t\tWRONG\n"); System.out.println( "The given files are not all identical! This means the content received by the participants was not the same."); return; } } } System.out.print("\t\t\t\t\t\t\t\t\t\tOK\n\n"); } else if (args.length == 1) { //Print a warning System.out.println( "BE CAREFUL: checking only one file ensure that the cryptographic values of the protocol are correct.\n" + "But, it does NOT ensure that all users have the same result. This could happen when a participant missed some messages.\n" + "This case is only covered by this verifier by giving multiple files as argument.\n"); } Serializer serializer = new Persister(); File source = new File(args[0]); poll = null; System.out.print("Reading file..."); try { poll = serializer.read(XMLPoll.class, source); System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\tOK\n"); } catch (Exception e) { System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\tFAILED\n"); e.printStackTrace(); return; } /* * Create the initializations needed by the protocol */ G_q = GStarModSafePrime.getInstance(new BigInteger(poll.getP())); Z_q = G_q.getZModOrder(); Zgroup = Z.getInstance(); generator = poll.getGenerator().getValue(G_q); representations = new Element[poll.getOptions().size()]; int i = 0; for (XMLOption op : poll.getOptions()) { representations[i] = op.getRepresentation().getValue(Z_q); i++; } System.out.println(); System.out.println("Verifying vote: \"" + poll.getQuestion() + "\""); /* * Verify the dependence between cryptographic values and the vote properties */ //Not used anymore, implicitly done with otherInputs of proofs //verifyDependencyTextCrypto(); /* * Verify the proofs for the user */ System.out.println(); System.out.println("Verifying the proof for the participants..."); a = new Element[poll.getParticipants().size()]; b = new Element[poll.getParticipants().size()]; h = new Element[poll.getParticipants().size()]; hHat = new Element[poll.getParticipants().size()]; hHatPowX = new Element[poll.getParticipants().size()]; proofForX = new Element[poll.getParticipants().size()]; validityProof = new Element[poll.getParticipants().size()]; equalityProof = new Element[poll.getParticipants().size()]; Tuple pollTuple = preparePollOtherInput(poll, representations, generator); int k = 0; for (XMLParticipant p : poll.getParticipants()) { System.out.println("\tParticipant " + p.getIdentification() + " (" + p.getUniqueId() + ")"); if (p.getAi() != null) { a[k] = p.getAi().getValue(G_q); if (DEBUG) System.out.println("Value a: " + a[k]); } if (p.getHi() != null) { h[k] = p.getHi().getValue(G_q); if (DEBUG) System.out.println("Value h: " + h[k]); } if (p.getBi() != null) { b[k] = p.getBi().getValue(G_q); if (DEBUG) System.out.println("Value b: " + b[k]); } if (p.getHiHat() != null) { hHat[k] = p.getHiHat().getValue(G_q); if (DEBUG) System.out.println("Value h hat: " + hHat[k]); } if (p.getHiHatPowXi() != null) { hHatPowX[k] = p.getHiHatPowXi().getValue(G_q); if (DEBUG) System.out.println("Value h hat ^ x: " + hHatPowX[k]); } if (p.getProofForXi() != null) { ProductGroup group = ProductGroup.getInstance(G_q, Zgroup, Z_q); proofForX[k] = p.getProofForXi().getValue(group); } if (p.getProofValidVote() != null) { Group[] tripleElement1Groups = new Group[poll.getOptions().size()]; Group[] tripleElement2Groups = new Group[poll.getOptions().size()]; Group[] tripleElement3Groups = new Group[poll.getOptions().size()]; for (i = 0; i < poll.getOptions().size(); i++) { tripleElement1Groups[i] = ProductGroup.getInstance(G_q, G_q); tripleElement2Groups[i] = Zgroup; tripleElement3Groups[i] = Z_q; } ProductGroup tripleElement1 = ProductGroup.getInstance(tripleElement1Groups); ProductGroup tripleElement2 = ProductGroup.getInstance(tripleElement2Groups); ProductGroup tripleElement3 = ProductGroup.getInstance(tripleElement3Groups); ProductGroup group = ProductGroup.getInstance(tripleElement1, tripleElement2, tripleElement3); validityProof[k] = p.getProofValidVote().getValue(group); if (DEBUG) System.out.println("Proof of validity: " + validityProof[k]); } if (p.getProofForHiHat() != null) { recoveryNeeded = true; ProductGroup group = ProductGroup.getInstance(ProductGroup.getInstance(G_q, G_q), Zgroup, Z_q); equalityProof[k] = p.getProofForHiHat().getValue(group); } Tuple participantTuple = prepareParticipantOtherInput(p); otherInput = Tuple.getInstance(participantTuple, pollTuple); /* * Verify proof of knowledge */ System.out.print("\t\tVerifying proof of knowledge of x value..."); verifyProofOfKnowledgeOfX(k); /* * Verify proof of validity */ if (validityProof[k] != null) { System.out.print("\t\tVerifying proof of valid vote..."); verifyValidityProof(k); } /* * Verify proof of equality */ if (equalityProof[k] != null) { System.out.print("\t\tVerifying proof of equality between discrete logarithm g^x and h_hat^x..."); verifyEqualityProof(k); } k++; } /** * Computing the result */ System.out.println(); System.out.print("Computing the result..."); computeResult(); }
From source file:Main.java
public static byte[] md5(byte[] bytes) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5Util"); md.update(bytes);// www . jav a 2 s.co m return md.digest(); }
From source file:Main.java
public static byte[] md5Byte(String encryptStr) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(encryptStr.getBytes("UTF-8")); return md.digest(); }