/** * Determine if the supplied hexadecimal string is potentially a binary key by checking the format of the string. * * @param hexadecimalStr the hexadecimal string; may be null * @return true if the supplied string is a properly formatted hexadecimal representation of a binary key, or false otherwise */ public static boolean isProperlyFormattedKey( String hexadecimalStr ) { if (hexadecimalStr == null) return false; // Length is expected to be the same as the digest ... final int length = hexadecimalStr.length(); if (length != ALGORITHM.getHexadecimalStringLength()) return false; // The characters all must be hexadecimal digits ... return StringUtil.isHexString(hexadecimalStr); }
protected byte[] computeHash( InputStream stream ) { try { return SecureHash.getHash(ALGORITHM, stream); } catch (NoSuchAlgorithmException e) { if (ALGORITHMS_NOT_FOUND_AND_LOGGED.add(ALGORITHM.digestName())) { Logger.getLogger(getClass()).error(e, GraphI18n.messageDigestNotFound, ALGORITHM.digestName()); } return NO_HASH; } catch (IOException e) { if (ALGORITHMS_NOT_FOUND_AND_LOGGED.add(ALGORITHM.digestName())) { Logger.getLogger(getClass()).error(e, GraphI18n.messageDigestNotFound, ALGORITHM.digestName()); } return NO_HASH; } }
private boolean isBinaryMetadataDocumentKey( String key ) { return key.endsWith("-ref") && Algorithm.SHA_1.isHexadecimal(key.substring(0, 40)); // 40 hexadecimal characters long }
/** * Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the * supplied name) as the returned Reader is used. This can be used to compute the hash while the content is being processed, * and saves from having to process the same content twice. * * @param algorithm the hashing function algorithm that should be used * @param reader the reader containing the content that is to be hashed * @param charset the character set used within the supplied reader; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingReader createHashingReader( Algorithm algorithm, Reader reader, Charset charset ) throws NoSuchAlgorithmException { return createHashingReader(algorithm.digestName(), reader, charset); }
/** * Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the * supplied name) as the returned Reader is used. This can be used to compute the hash while the content is being processed, * and saves from having to process the same content twice. * * @param algorithm the hashing function algorithm that should be used * @param reader the reader containing the content that is to be hashed * @param charset the character set used within the supplied reader; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingReader createHashingReader( Algorithm algorithm, Reader reader, Charset charset ) throws NoSuchAlgorithmException { return createHashingReader(algorithm.digestName(), reader, charset); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param stream the stream containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the stream */ public static byte[] getHash( Algorithm algorithm, InputStream stream ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), stream); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param stream the stream containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the stream */ public static byte[] getHash( Algorithm algorithm, InputStream stream ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), stream); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param file the file containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the file */ public static byte[] getHash( Algorithm algorithm, File file ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), file); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param stream the stream containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the stream */ public static byte[] getHash( Algorithm algorithm, InputStream stream ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), stream); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param file the file containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the file */ public static byte[] getHash( Algorithm algorithm, File file ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), file); }
/** * Create an Reader instance that wraps another reader and that computes the secure hash (using the algorithm with the * supplied name) as the returned Reader is used. This can be used to compute the hash while the content is being processed, * and saves from having to process the same content twice. * * @param algorithm the hashing function algorithm that should be used * @param reader the reader containing the content that is to be hashed * @param charset the character set used within the supplied reader; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingReader createHashingReader( Algorithm algorithm, Reader reader, Charset charset ) throws NoSuchAlgorithmException { return createHashingReader(algorithm.digestName(), reader, charset); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param file the file containing the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null * @throws IOException if there is an error reading the file */ public static byte[] getHash( Algorithm algorithm, File file ) throws NoSuchAlgorithmException, IOException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), file); }
protected byte[] computeHash( File file ) { try { return SecureHash.getHash(ALGORITHM, file); } catch (NoSuchAlgorithmException e) { if (ALGORITHMS_NOT_FOUND_AND_LOGGED.add(ALGORITHM.digestName())) { Logger.getLogger(getClass()).error(e, GraphI18n.messageDigestNotFound, ALGORITHM.digestName()); } return NO_HASH; } catch (IOException e) { if (ALGORITHMS_NOT_FOUND_AND_LOGGED.add(ALGORITHM.digestName())) { Logger.getLogger(getClass()).error(e, GraphI18n.messageDigestNotFound, ALGORITHM.digestName()); } return NO_HASH; } }
/** * Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the * supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being * processed by another reader, and saves from having to process the same stream twice. * * @param algorithm the hashing function algorithm that should be used * @param inputStream the stream containing the content that is to be hashed * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingInputStream createHashingStream( Algorithm algorithm, InputStream inputStream ) throws NoSuchAlgorithmException { return createHashingStream(algorithm.digestName(), inputStream); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param content the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null */ public static byte[] getHash( Algorithm algorithm, byte[] content ) throws NoSuchAlgorithmException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), content); }
/** * Get the hash of the supplied content, using the supplied digest algorithm. * * @param algorithm the hashing function algorithm that should be used * @param content the content to be hashed; may not be null * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException if the supplied algorithm could not be found * @throws IllegalArgumentException if the algorithm is null */ public static byte[] getHash( Algorithm algorithm, byte[] content ) throws NoSuchAlgorithmException { CheckArg.isNotNull(algorithm, "algorithm"); return getHash(algorithm.digestName(), content); }
public static int maxHexadecimalLength() { return ALGORITHM.getHexadecimalStringLength(); }
public static int maxHexadecimalLength() { return ALGORITHM.getHexadecimalStringLength(); }
/** * Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the * supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being * processed by another reader, and saves from having to process the same stream twice. * * @param algorithm the hashing function algorithm that should be used * @param inputStream the stream containing the content that is to be hashed * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingInputStream createHashingStream( Algorithm algorithm, InputStream inputStream ) throws NoSuchAlgorithmException { return createHashingStream(algorithm.digestName(), inputStream); }
/** * Determine if the supplied hexadecimal string is potentially a binary key by checking the format of the string. * * @param hexadecimalStr the hexadecimal string; may be null * @return true if the supplied string is a properly formatted hexadecimal representation of a binary key, or false otherwise */ public static boolean isProperlyFormattedKey( String hexadecimalStr ) { if (hexadecimalStr == null) return false; // Length is expected to be the same as the digest ... final int length = hexadecimalStr.length(); if (length != ALGORITHM.getHexadecimalStringLength()) return false; // The characters all must be hexadecimal digits ... return StringUtil.isHexString(hexadecimalStr); }