List of usage examples for java.security DigestInputStream DigestInputStream
public DigestInputStream(InputStream stream, MessageDigest digest)
From source file:MainClass.java
public static void main(String args[]) throws Exception { MessageDigest m = MessageDigest.getInstance("MD5"); FileInputStream fin = new FileInputStream(args[0]); DigestInputStream din = new DigestInputStream(fin, m); while (din.read() != -1) ;//w w w. j a v a 2 s . c o m byte s[] = m.digest(); for (int i = 0; i < s.length; i++) { System.out.print(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6)); } }
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();// w w w .j ava 2s . com 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:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); System.out.println("input : " + new String(input)); MessageDigest hash = MessageDigest.getInstance("SHA1"); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input); DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int ch;/*w ww . ja va2 s .c om*/ while ((ch = digestInputStream.read()) >= 0) { byteArrayOutputStream.write(ch); } byte[] newInput = byteArrayOutputStream.toByteArray(); System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest())); byteArrayOutputStream = new ByteArrayOutputStream(); DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash); digestOutputStream.write(newInput); digestOutputStream.close(); System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest())); }
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);/*w w w .j a v a 2 s. c o 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: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 *///from w ww . ja v a 2s. c om 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 boolean checkMd5sum(File file, String checkCode) throws IOException { DigestInputStream dInput = null; try {/* w ww.ja va 2 s . co m*/ FileInputStream fInput = new FileInputStream(file); dInput = new DigestInputStream(fInput, getMd5Instance()); byte[] buf = new byte[8192]; while (dInput.read(buf) > 0) { } byte[] bytes = dInput.getMessageDigest().digest(); return bytes2hex(bytes).equals(checkCode); } finally { closeQuietly(dInput); } }
From source file:MainClass.java
public static String md(String f) throws Exception { BufferedInputStream file = new BufferedInputStream(new FileInputStream(f)); MessageDigest md = MessageDigest.getInstance("MD5"); DigestInputStream in = new DigestInputStream(file, md); int i;//from ww w . j a va2s .c o m byte[] buffer = new byte[BUFFER_SIZE]; do { i = in.read(buffer, 0, BUFFER_SIZE); } while (i == BUFFER_SIZE); md = in.getMessageDigest(); in.close(); return new String(md.digest()); }
From source file:MainClass.java
public static String md(String f) throws Exception { BufferedInputStream file = new BufferedInputStream(new FileInputStream(f)); MessageDigest md = MessageDigest.getInstance("SHA-1"); DigestInputStream in = new DigestInputStream(file, md); int i;/*from w w w. j av a 2s. c om*/ byte[] buffer = new byte[BUFFER_SIZE]; do { i = in.read(buffer, 0, BUFFER_SIZE); } while (i == BUFFER_SIZE); md = in.getMessageDigest(); in.close(); return new String(md.digest()); }
From source file:Main.java
public static String getMD5(String file) { String md5 = ""; try {/*from w w w.j ava2 s . c om*/ MessageDigest md = MessageDigest.getInstance("MD5"); InputStream is; is = new FileInputStream(file); DigestInputStream dis = new DigestInputStream(is, md); byte data[] = new byte[1024]; @SuppressWarnings("unused") int count; while ((count = dis.read(data)) != -1) { } byte[] digest = md.digest(); for (int i = 0; i < digest.length; i++) { md5 += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1); } return md5; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return md5; }
From source file:MainClass.java
static void performInputTest() throws Exception { MessageDigest md = MessageDigest.getInstance("SHA"); FileInputStream fin = new FileInputStream("sha-results.txt"); DigestInputStream in = new DigestInputStream(fin, md); byte[] b = new byte["testCase".getBytes().length]; in.read(b, 0, "testCase".getBytes().length); md = in.getMessageDigest();// w w w. j ava 2 s . c om String s = new String(md.digest()); System.out.println("Calculated result: " + s); }