List of usage examples for java.security DigestInputStream read
public int read() throws IOException
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) ;//from w ww. ja v a 2 s. co 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: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;//from ww w.j av a 2 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: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 *//*www.ja va 2 s. com*/ 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:MainClass.java
static private String computeDigest(MessageDigest algorithm, String filename) { String returnValue = ""; try {// www . j a va 2 s .c o m algorithm.reset(); FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); DigestInputStream dis = new DigestInputStream(bis, algorithm); int ch; while ((ch = dis.read()) != -1) ; StringBuffer hexString = new StringBuffer(); byte digest[] = algorithm.digest(); int digestLength = digest.length; for (int i = 0; i < digestLength; i++) { hexString.append(hexDigit(digest[i])); hexString.append(" "); } returnValue = hexString.toString(); } catch (IOException e) { System.err.println("Error generating digest for: " + filename); } return returnValue; }
From source file:edu.ku.brc.specify.plugins.ipadexporter.MD5Checksum.java
/** * @param file/*from ww w .ja v a 2 s.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:org.rhq.gui.content.YumMetadata.java
protected static String getSHA(StringBuffer primaryXML) throws NoSuchAlgorithmException, IOException { // Note: For RHEL6 this may need to change to "SHA-256", // going for SHA for now, as it's a lowest common denominator so it can work with older clients. MessageDigest md = MessageDigest.getInstance("SHA"); ByteArrayInputStream bis = new ByteArrayInputStream(primaryXML.toString().getBytes()); DigestInputStream mdistr = new DigestInputStream(bis, md); while (mdistr.read() != -1) { ;/*from ww w. j a v a 2s.c o m*/ } mdistr.close(); return Hex.encodeHexString(md.digest()); }
From source file:com.codemarvels.ant.aptrepotask.utils.Utils.java
/** * Compute the given message digest for a file. * /*from ww w . j ava 2s. com*/ * @param hashType algorithm to be used (as {@code String}) * @param file File to compute the digest for (as {@code File}). * @return A {@code String} for the hex encoded digest. * @throws MojoExecutionException */ public static String getDigest(String hashType, File file) { try { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); MessageDigest digest = MessageDigest.getInstance(hashType); DigestInputStream dis = new DigestInputStream(bis, digest); @SuppressWarnings("unused") int ch; while ((ch = dis.read()) != -1) ; String hex = new String(Hex.encodeHex(digest.digest())); fis.close(); bis.close(); dis.close(); return hex; } catch (NoSuchAlgorithmException e) { throw new RuntimeException("could not create digest", e); } catch (FileNotFoundException e) { throw new RuntimeException("could not create digest", e); } catch (IOException e) { throw new RuntimeException("could not create digest", e); } }
From source file:org.m1theo.apt.repo.utils.Utils.java
/** * Compute the given message digest for a file. * //www. j a va 2s. co m * @param hashType algorithm to be used (as {@code String}) * @param file File to compute the digest for (as {@code File}). * @return A {@code String} for the hex encoded digest. * @throws AptRepoException */ public static String getDigest(String hashType, File file) throws AptRepoException { try { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); MessageDigest digest = MessageDigest.getInstance(hashType); DigestInputStream dis = new DigestInputStream(bis, digest); @SuppressWarnings("unused") int ch; while ((ch = dis.read()) != -1) ; String hex = new String(Hex.encodeHex(digest.digest())); fis.close(); bis.close(); dis.close(); return hex; } catch (NoSuchAlgorithmException e) { throw new AptRepoException("could not create digest", e); } catch (FileNotFoundException e) { throw new AptRepoException("could not create digest", e); } catch (IOException e) { throw new AptRepoException("could not create digest", e); } }
From source file:com.stimulus.archiva.domain.EmailID.java
public static synchronized String generateUniqueID(Email email) { try {//from w ww . j a va2 s. co m MimeMessage raw = email; // we need a backup plan here if (raw == null) { return DateUtil.convertDatetoString(new Date()); } Enumeration<Header> headers = raw.getAllHeaders(); LinkedList<String> orderedHeaders = new LinkedList<String>(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); if (Compare.equalsIgnoreCase(header.getName(), "Date") || Compare.equalsIgnoreCase(header.getName(), "CC") || Compare.equalsIgnoreCase(header.getName(), "BCC") || Compare.equalsIgnoreCase(header.getName(), "Subject") || Compare.equalsIgnoreCase(header.getName(), "To") || Compare.equalsIgnoreCase(header.getName(), "From")) orderedHeaders.add(header.getName() + header.getValue()); } Collections.sort(orderedHeaders); StringBuffer allHeaders = new StringBuffer(); for (String header : orderedHeaders) allHeaders.append(header); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] bytes = allHeaders.toString().getBytes(); InputStream is = new ByteArrayInputStream(bytes); DigestInputStream dis = new DigestInputStream(is, sha); while (dis.read() != -1) ; dis.close(); byte[] digest = sha.digest(); return toHex(digest); } catch (Exception e) { logger.error("failed to generate a uniqueid for a message"); return null; } }
From source file:com.vmware.identity.idm.CommonUtil.java
public static String Computesha256Hash(File secretFile) throws IOException, NoSuchAlgorithmException { FileInputStream fs = null;//w ww . ja va 2s . c o m try { fs = new FileInputStream(secretFile); MessageDigest md = MessageDigest.getInstance("SHA-256"); DigestInputStream dis = new DigestInputStream(fs, md); while ((dis.read()) != -1) ; BigInteger bi = new BigInteger(md.digest()); return bi.toString(64); } finally { if (fs != null) { fs.close(); } } }