org.littleshoot.util
Class Sha1Hasher

java.lang.Object
  extended by org.littleshoot.util.Sha1Hasher

public final class Sha1Hasher
extends Object

Utility class for generating SHA-1 file hashes. Formats SHA-1s as URNs, as specified in RFC 2141 and according to MAGNET conventions.


Method Summary
static URI createSha1Urn(byte[] buf)
          Create a new SHA1 hash URI for the specified ByteBuffer.
static URI createSha1Urn(ByteBuffer buf)
          Create a new SHA1 hash URI for the specified ByteBuffer.
static URI createSha1Urn(File file)
          Create a new SHA1 hash URI for the specified file on disk.
static URI createSha1Urn(InputStream is)
          Create a new SHA1 hash URI for the specified InputStream.
static URI createSha1Urn(InputStream is, MessageDigest md)
          Create a new SHA1 hash URI for the specified InputStream.
static String hash(String str)
          Creates a base 64 encoded SHA-1 hash of the specified string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createSha1Urn

public static URI createSha1Urn(File file)
                         throws IOException
Create a new SHA1 hash URI for the specified file on disk.

Parameters:
file - the file to construct the hash from
Returns:
The SHA1 hash as a URI.
Throws:
IOException - If there is an error creating the hash or if the specified algorithm cannot be found.

createSha1Urn

public static URI createSha1Urn(byte[] buf)
                         throws IOException
Create a new SHA1 hash URI for the specified ByteBuffer.

Parameters:
buf - The bytes to create the SHA-1 for.
Returns:
The SHA1 hash as a URI.
Throws:
IOException - If there is an error creating the hash or if the specified algorithm cannot be found.

createSha1Urn

public static URI createSha1Urn(ByteBuffer buf)
                         throws IOException
Create a new SHA1 hash URI for the specified ByteBuffer.

Parameters:
buf - The ByteBuffer to create the SHA-1 for.
Returns:
The SHA1 hash as a URI.
Throws:
IOException - If there is an error creating the hash or if the specified algorithm cannot be found.

createSha1Urn

public static URI createSha1Urn(InputStream is)
                         throws IOException
Create a new SHA1 hash URI for the specified InputStream.

Parameters:
is - The InputStream to create the SHA-1 for.
Returns:
The SHA1 hash as a URI.
Throws:
IOException - If there is an error creating the hash or if the specified algorithm cannot be found.

createSha1Urn

public static URI createSha1Urn(InputStream is,
                                MessageDigest md)
                         throws IOException
Create a new SHA1 hash URI for the specified InputStream.

Parameters:
is - The InputStream to create the SHA-1 for.
md - The MessageDigest class to use.
Returns:
The SHA1 hash as a URI.
Throws:
IOException - If there is an error creating the hash or if the specified algorithm cannot be found.

hash

public static String hash(String str)
Creates a base 64 encoded SHA-1 hash of the specified string.

Parameters:
str - The string to hash.
Returns:
The hashed and encoded string.


Copyright © 2011-2013 LittleShoot. All Rights Reserved.