Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
albertattard committed Feb 4, 2015
1 parent 0ebf362 commit cbb327c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crypto/How to Compute Hash Code of Streams/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.javacreed.examples</groupId>
<artifactId>how-to-compute-hash-code-of-large-files</artifactId>
<artifactId>how-to-compute-hash-code-of-streams</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>How to Compute Hash Code of Large Files</name>
<name>How to Compute Hash Code of Streams</name>
<url>http://www.javacreed.com/${project.artifactId}/</url>
<inceptionYear>${project.inceptionYear}</inceptionYear>
<organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* #%L
* How to Compute Hash Code of Streams
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2012 - 2015 Java Creed
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.javacreed.examples.crypto;

import java.io.BufferedInputStream;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Formatter;

public class Example {
public static void main(final String[] args) throws Exception {

final URL link = new URL("http://www.javacreed.com/");
try (DigestInputStream in = new DigestInputStream(new BufferedInputStream(link.openStream()),
MessageDigest.getInstance("SHA-1"))) {
// Read the stream and do nothing with it
while (in.read() != -1) {}

// Get the digest and finialise the computation
final MessageDigest md = in.getMessageDigest();
final byte[] digest = md.digest();

// Format as HEX
try (Formatter formatter = new Formatter()) {
for (final byte b : digest) {
formatter.format("%02x", b);
}

final String sha1 = formatter.toString();
System.out.println(sha1);
}
}
}
}

0 comments on commit cbb327c

Please sign in to comment.