use of java.security.MessageDigest in project hadoop by apache.
the class Signer method computeSignature.
/**
* Returns then signature of a string.
*
* @param secret The secret to use
* @param str string to sign.
*
* @return the signature for the string.
*/
protected String computeSignature(byte[] secret, String str) {
try {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(str.getBytes(Charset.forName("UTF-8")));
md.update(secret);
byte[] digest = md.digest();
return new Base64(0).encodeToString(digest);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("It should not happen, " + ex.getMessage(), ex);
}
}
use of java.security.MessageDigest in project flink by apache.
the class BlobClient method putInputStream.
/**
* Uploads data from the given input stream to the BLOB server.
*
* @param jobId
* the ID of the job the BLOB belongs to or <code>null</code> to store the BLOB in a content-addressable
* manner
* @param key
* the key to identify the BLOB on the server or <code>null</code> to store the BLOB in a content-addressable
* manner
* @param inputStream
* the input stream to read the data from
* @return he computed BLOB key if the BLOB has been stored in a content-addressable manner, <code>null</code>
* otherwise
* @throws IOException
* thrown if an I/O error occurs while uploading the data to the BLOB server
*/
private BlobKey putInputStream(JobID jobId, String key, InputStream inputStream) throws IOException {
if (this.socket.isClosed()) {
throw new IllegalStateException("BLOB Client is not connected. " + "Client has been shut down or encountered an error before.");
}
if (LOG.isDebugEnabled()) {
if (jobId == null) {
LOG.debug(String.format("PUT content addressable BLOB stream to %s", socket.getLocalSocketAddress()));
} else {
LOG.debug(String.format("PUT BLOB stream under %s / \"%s\" to %s", jobId, key, socket.getLocalSocketAddress()));
}
}
try {
final OutputStream os = this.socket.getOutputStream();
final MessageDigest md = jobId == null ? BlobUtils.createMessageDigest() : null;
final byte[] xferBuf = new byte[BUFFER_SIZE];
// Send the PUT header
sendPutHeader(os, jobId, key);
while (true) {
final int read = inputStream.read(xferBuf);
if (read < 0) {
// we are done. send a -1 and be done
writeLength(-1, os);
break;
}
if (read > 0) {
writeLength(read, os);
os.write(xferBuf, 0, read);
if (md != null) {
md.update(xferBuf, 0, read);
}
}
}
// Receive blob key and compare
final InputStream is = this.socket.getInputStream();
return receivePutResponseAndCompare(is, md);
} catch (Throwable t) {
BlobUtils.closeSilently(socket, LOG);
throw new IOException("PUT operation failed: " + t.getMessage(), t);
}
}
use of java.security.MessageDigest in project flink by apache.
the class BlobClientSslTest method prepareTestFile.
/**
* Prepares a test file for the unit tests, i.e. the methods fills the file with a particular byte patterns and
* computes the file's BLOB key.
*
* @param file
* the file to prepare for the unit tests
* @return the BLOB key of the prepared file
* @throws IOException
* thrown if an I/O error occurs while writing to the test file
*/
private static BlobKey prepareTestFile(File file) throws IOException {
MessageDigest md = BlobUtils.createMessageDigest();
final byte[] buf = new byte[TEST_BUFFER_SIZE];
for (int i = 0; i < buf.length; ++i) {
buf[i] = (byte) (i % 128);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
for (int i = 0; i < 20; ++i) {
fos.write(buf);
md.update(buf);
}
} finally {
if (fos != null) {
fos.close();
}
}
return new BlobKey(md.digest());
}
use of java.security.MessageDigest in project flink by apache.
the class BlobClientTest method testContentAddressableBuffer.
/**
* Tests the PUT/GET operations for content-addressable buffers.
*/
@Test
public void testContentAddressableBuffer() {
BlobClient client = null;
try {
byte[] testBuffer = createTestBuffer();
MessageDigest md = BlobUtils.createMessageDigest();
md.update(testBuffer);
BlobKey origKey = new BlobKey(md.digest());
InetSocketAddress serverAddress = new InetSocketAddress("localhost", BLOB_SERVER.getPort());
client = new BlobClient(serverAddress, blobServiceConfig);
// Store the data
BlobKey receivedKey = client.put(testBuffer);
assertEquals(origKey, receivedKey);
// Retrieve the data
InputStream is = client.get(receivedKey);
validateGet(is, testBuffer);
// Check reaction to invalid keys
try {
client.get(new BlobKey());
fail("Expected IOException did not occur");
} catch (IOException fnfe) {
// expected
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (client != null) {
try {
client.close();
} catch (Throwable t) {
}
}
}
}
use of java.security.MessageDigest in project flink by apache.
the class BlobClientTest method prepareTestFile.
/**
* Prepares a test file for the unit tests, i.e. the methods fills the file with a particular byte patterns and
* computes the file's BLOB key.
*
* @param file
* the file to prepare for the unit tests
* @return the BLOB key of the prepared file
* @throws IOException
* thrown if an I/O error occurs while writing to the test file
*/
private static BlobKey prepareTestFile(File file) throws IOException {
MessageDigest md = BlobUtils.createMessageDigest();
final byte[] buf = new byte[TEST_BUFFER_SIZE];
for (int i = 0; i < buf.length; ++i) {
buf[i] = (byte) (i % 128);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
for (int i = 0; i < 20; ++i) {
fos.write(buf);
md.update(buf);
}
} finally {
if (fos != null) {
fos.close();
}
}
return new BlobKey(md.digest());
}
Aggregations