Search in sources :

Example 1 with MessageDigest

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);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 2 with MessageDigest

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);
    }
}
Also used : FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 3 with MessageDigest

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());
}
Also used : FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest)

Example 4 with MessageDigest

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) {
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) IOException(java.io.IOException) EOFException(java.io.EOFException) Test(org.junit.Test)

Example 5 with MessageDigest

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());
}
Also used : FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest)

Aggregations

MessageDigest (java.security.MessageDigest)1122 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)562 IOException (java.io.IOException)161 UnsupportedEncodingException (java.io.UnsupportedEncodingException)93 BigInteger (java.math.BigInteger)93 FileInputStream (java.io.FileInputStream)68 InputStream (java.io.InputStream)66 File (java.io.File)58 DigestInputStream (java.security.DigestInputStream)56 Test (org.junit.Test)55 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 DigestOutputStream (java.security.DigestOutputStream)43 ArrayList (java.util.ArrayList)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 OutputStream (java.io.OutputStream)27 X509Certificate (java.security.cert.X509Certificate)27 GeneralSecurityException (java.security.GeneralSecurityException)25 Cipher (javax.crypto.Cipher)23 Provider (java.security.Provider)22 SecretKeySpec (javax.crypto.spec.SecretKeySpec)22