/** * {@inheritDoc} * * @see org.modeshape.common.text.TextEncoder#encode(java.lang.String) */ @Override public String encode( String text ) { try { byte[] hash = SecureHash.getHash(algorithm, text.getBytes()); String result = Base64.encodeBytes(hash); return result.length() < maxLength ? result : result.substring(0, maxLength); } catch (NoSuchAlgorithmException e) { return text; } }
/** * 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); }
/** * Computes the sha1 value for the given string. * * @param string a non-null string * @return the SHA1 value for the given string. */ public static String sha1( String string ) { try { byte[] sha1 = SecureHash.getHash(SecureHash.Algorithm.SHA_1, string.getBytes()); return SecureHash.asHexString(sha1); } catch (NoSuchAlgorithmException e) { throw new SystemFailureException(e); } }
protected void assertSecureHashStreamWorks( Algorithm algorithm, String resourceName ) throws IOException, NoSuchAlgorithmException { // Find the content of the file ... InputStream stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); byte[] bytesThruStream = IoUtil.readBytes(stream); // Find the secure hash of the file ... stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); byte[] hashThruStream = null; try { hashThruStream = SecureHash.getHash(algorithm, stream); } finally { stream.close(); } // Now try reading the stream using a hash stream ... stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); HashingInputStream hashingStream = SecureHash.createHashingStream(algorithm, stream); byte[] bytesThruHashingStream = IoUtil.readBytes(hashingStream); // closes stream byte[] hashThruHashingStream = hashingStream.getHash(); // The content should be the same .. assertThat(bytesThruHashingStream, is(bytesThruStream)); // The hash should also be the same ... assertThat(hashThruHashingStream, is(hashThruStream)); // System.out.println(algorithm.digestName() + "---> " + hashingStream.getHashAsHexString() + " of " + resourceName); }
/** * Computes the SHA1 for the given file. By default, this method will look at the * {@link FileSystemConnector#contentBasedSha1()} flag and either take the URL of the file (using @see * java.util.File#toURI().toURL() and return the SHA1 of the URL string or return the SHA1 of the entire file content. * * @param file a {@link File} instance; never null * @return the SHA1 of the file. */ protected String sha1( File file ) { try { if (contentBasedSha1()) { byte[] hash = SecureHash.getHash(SecureHash.Algorithm.SHA_1, file); return StringUtil.getHexString(hash); } return SecureHash.sha1(createUrlForFile(file).toString()); } catch (Exception e) { throw new ConnectorException(e); } }
/** * Get the string representation of the binary hash of the content read by this reader. This method will return null if * the reader has not yet been closed. * * @return the hex-encoded representation of the binary hash of the contents, or null if the reader has not yet been * closed */ public String getHashAsHexString() { return SecureHash.asHexString(hash); } }
protected BucketId( String nameString, int length ) { String sha1 = SecureHash.sha1(nameString); this.id = sha1.substring(0, length); }
/** * 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); }
/** * Computes the SHA1 for the given file. By default, this method will look at the * {@link FileSystemConnector#contentBasedSha1()} flag and either take the URL of the file (using @see * java.util.File#toURI().toURL() and return the SHA1 of the URL string or return the SHA1 of the entire file content. * * @param file a {@link File} instance; never null * @return the SHA1 of the file. */ protected String sha1( File file ) { try { if (contentBasedSha1()) { byte[] hash = SecureHash.getHash(SecureHash.Algorithm.SHA_1, file); return StringUtil.getHexString(hash); } return SecureHash.sha1(createUrlForFile(file).toString()); } catch (Exception e) { throw new ConnectorException(e); } }
/** * Get the string representation of the binary hash of the content read by this stream. This method will return null if * the stream has not yet been closed. * * @return the hex-encoded representation of the binary hash of the contents, or null if the stream has not yet been * closed */ public String getHashAsHexString() { return SecureHash.asHexString(hash); } }
protected BucketId( String nameString, int length ) { String sha1 = SecureHash.sha1(nameString); this.id = sha1.substring(0, length); }
/** * 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); }
/** * {@inheritDoc} * * @see org.modeshape.common.text.TextEncoder#encode(java.lang.String) */ @Override public String encode( String text ) { try { byte[] hash = SecureHash.getHash(algorithm, text.getBytes()); String result = Base64.encodeBytes(hash); return result.length() < maxLength ? result : result.substring(0, maxLength); } catch (NoSuchAlgorithmException e) { return text; } }
/** * Computes the sha1 value for the given string. * * @param string a non-null string * @return the SHA1 value for the given string. */ public static String sha1( String string ) { try { byte[] sha1 = SecureHash.getHash(SecureHash.Algorithm.SHA_1, string.getBytes()); return SecureHash.asHexString(sha1); } catch (NoSuchAlgorithmException e) { throw new SystemFailureException(e); } }
/** * 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 string representation of the binary hash of the content read by this reader. This method will return null if * the reader has not yet been closed. * * @return the hex-encoded representation of the binary hash of the contents, or null if the reader has not yet been * closed */ public String getHashAsHexString() { return SecureHash.asHexString(hash); } }
public static String keyForSourceName( String name ) { return SecureHash.sha1(name).substring(0, NodeKey.SOURCE_LENGTH); }
/** * 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); }
/** * {@inheritDoc} * * @see org.modeshape.common.text.TextEncoder#encode(java.lang.String) */ @Override public String encode( String text ) { try { byte[] hash = SecureHash.getHash(algorithm, text.getBytes()); String result = Base64.encodeBytes(hash); return result.length() < maxLength ? result : result.substring(0, maxLength); } catch (NoSuchAlgorithmException e) { return text; } }
/** * Computes the sha1 value for the given string. * * @param string a non-null string * @return the SHA1 value for the given string. */ public static String sha1( String string ) { try { byte[] sha1 = SecureHash.getHash(SecureHash.Algorithm.SHA_1, string.getBytes()); return SecureHash.asHexString(sha1); } catch (NoSuchAlgorithmException e) { throw new SystemFailureException(e); } }