Example usage for java.security DigestInputStream read

List of usage examples for java.security DigestInputStream read

Introduction

In this page you can find the example usage for java.security DigestInputStream read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte, and updates the message digest (if the digest function is on).

Usage

From source file:com.clustercontrol.repository.factory.AgentLibDownloader.java

/**
 * MD5??//ww  w  .jav a2s  .  c om
 * @param filepath
 * @return
 * @throws HinemosUnknown 
 */
private static String getMD5(String filepath) throws HinemosUnknown {
    MessageDigest md = null;
    DigestInputStream inStream = null;
    byte[] digest = null;
    try {
        md = MessageDigest.getInstance("MD5");
        inStream = new DigestInputStream(new BufferedInputStream(new FileInputStream(filepath)), md);
        while (inStream.read() != -1) {
        }
        digest = md.digest();
    } catch (Exception e) {
        m_log.warn("getMD5() : filepath=" + filepath + ", " + e.getClass(), e);
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (Exception e) {
                m_log.warn("getMD5() : close " + e.getClass(), e);
            }
        }
        if (digest == null)
            throw new HinemosUnknown("MD5 digest is null");
    }
    return hashByte2MD5(digest);
}

From source file:at.alladin.rmbt.client.v2.task.HttpProxyTask.java

/**
 * //from  w ww.j  a  v a 2  s  .  c om
 * @param file
 * @return
 * @throws NoSuchAlgorithmException
 * @throws IOException 
 */
public static String generateChecksum(File file) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream dis = new DigestInputStream(new FileInputStream(file), md);
    int ch;
    while ((ch = dis.read()) != -1) {
        //empty block
    }
    dis.close();
    return generateChecksumFromDigest(md.digest());
}

From source file:org.eclipse.kura.deployment.rp.sh.impl.ShellScriptResourceProcessorImpl.java

private static byte[] computeDigest(InputStream is) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream dis = new DigestInputStream(is, md);
    while (dis.read() != -1) {
        ;//from w w w.j  av  a2  s. c o m
    }
    byte[] digest = md.digest();
    return digest;
}

From source file:de.uzk.hki.da.pkg.MetsConsistencyChecker.java

/**
 * Calculate hash.//from   w ww. ja  v a2 s.  c om
 *
 * @param algorithm the algorithm
 * @param file the file
 * @return the string
 * @throws Exception the exception
 */
private static String calculateHash(MessageDigest algorithm, File file) throws Exception {

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, algorithm);

    // read the file and update the hash calculation
    while (dis.read() != -1)
        ;

    // get the hash value as byte array
    byte[] hash = algorithm.digest();

    fis.close();
    bis.close();
    dis.close();

    return byteArray2Hex(hash);

}

From source file:fi.mikuz.boarder.util.FileProcessor.java

public static String calculateHash(File file) throws Exception {

    MessageDigest algorithm = MessageDigest.getInstance("SHA1");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, algorithm);

    // read the file and update the hash calculation
    while (dis.read() != -1)
        ;//from www .  j av a 2  s .  c  o m

    // get the hash value as byte array
    byte[] hash = algorithm.digest();

    return byteArray2Hex(hash);
}

From source file:com.github.nlloyd.hornofmongo.MongoScope.java

public static Object md5sumFile(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    assertSingleArgument(args);//from  w  ww  . j  a  v a 2 s  .  co m
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
    File inFile;
    try {
        inFile = resolveFilePath((MongoScope) thisObj, Context.toString(args[0])).getCanonicalFile();
    } catch (IOException e) {
        Context.throwAsScriptRuntimeEx(e);
        return null;
    }
    InputStream in = null;
    DigestInputStream dis = null;
    try {
        in = new BufferedInputStream(new FileInputStream(inFile));
        dis = new DigestInputStream(in, md);
        while (dis.available() > 0)
            dis.read();
        byte[] digest = md.digest();
        String hexStr = Hex.encodeHexString(digest);
        return hexStr;
    } catch (FileNotFoundException e) {
        Context.throwAsScriptRuntimeEx(e);
    } catch (IOException e) {
        Context.throwAsScriptRuntimeEx(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
            }
        }
    }
    return Undefined.instance;
}

From source file:org.openintents.safe.CryptoHelper.java

/**
 * @param message/*  ww  w.  ja v a  2 s. c  o  m*/
 * @return MD5 digest of message in a byte array
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public static byte[] md5String(String message) {

    byte[] input = message.getBytes();

    MessageDigest hash;
    ByteArrayInputStream bIn = null;
    DigestInputStream dIn = null;

    try {
        hash = MessageDigest.getInstance("MD5");

        bIn = new ByteArrayInputStream(input);
        dIn = new DigestInputStream(bIn, hash);

        for (int i = 0; i < input.length; i++) {
            dIn.read();
        }

    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "md5String(): " + e.toString());
    } catch (IOException e) {
        Log.e(TAG, "md5String(): " + e.toString());
    }

    return dIn.getMessageDigest().digest();
}

From source file:org.duracloud.chunk.ChunkableContentTest.java

private void read(DigestInputStream istream) throws IOException {
    while (istream.read() != -1) {
        // walk through the stream
    }/* w  w w.j  a v  a 2  s.c om*/
}

From source file:org.panbox.linux.desktop.identitymgmt.TestDownloadHashCompare.java

/**
 * Creates a Hash value for a given file
 * @param file// ww w .j a v a 2 s  .c  o m
 * @return
 */
private byte[] createFileHash(File file) {
    MessageDigest md = null;
    try (InputStream is = Files.newInputStream(Paths.get(file.getAbsolutePath()))) {

        md = MessageDigest.getInstance("SHA-256");
        DigestInputStream dis = new DigestInputStream(is, md);

        /* Read stream to EOF as normal... */

        while (dis.available() > 0) {
            dis.read();
        }

    } catch (NoSuchAlgorithmException e) {
        fail();
    } catch (IOException e1) {
        fail();
    }
    byte[] digest = md.digest();

    //String h = Hex.encodeHexString( digest );

    return digest;
}

From source file:org.seadva.dataone.DataOneUtil.java

@GET
@Path("addObject")
@Produces(MediaType.APPLICATION_JSON)/*from   w w w .j  av  a  2 s.co  m*/
public String addObject(@QueryParam("filePath") String filePath, @QueryParam("id") String identifier,
        @QueryParam("schema") String metaFormat) throws IndexServiceException {

    ResearchObject researchObject = new ResearchObject();

    String filename = ((String) filePath).split("/")[((String) filePath).split("/").length - 1].replace(".xml",
            "");
    if (metaFormat == null)
        metaFormat = "http://www.fgdc.gov/schemas/metadata/fgdc-std-001-1998.xsd";
    if (identifier == null)
        identifier = filename;

    SeadFile metadataFile = new SeadFile();
    metadataFile.setId(filename);
    metadataFile.setName(filename);
    metadataFile.setSource("file://" + filePath);

    try {
        DigestInputStream digestStream = new DigestInputStream(new FileInputStream(filePath),
                MessageDigest.getInstance("SHA-1"));
        if (digestStream.read() != -1) {
            byte[] buf = new byte[1024];
            while (digestStream.read(buf) != -1)
                ;
        }
        byte[] digest = digestStream.getMessageDigest().digest();
        DcsFixity fixity = new DcsFixity();
        fixity.setAlgorithm("SHA-1");
        fixity.setValue(new String(Hex.encodeHex(digest)));
        metadataFile.addFixity(fixity);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    DcsFormat metadataFormat = new DcsFormat();
    metadataFormat.setFormat(metaFormat);
    metadataFile.addFormat(metadataFormat);

    DcsResourceIdentifier dcsResourceIdentifier = new DcsResourceIdentifier();
    dcsResourceIdentifier.setIdValue(identifier);
    dcsResourceIdentifier.setTypeId("dataone");
    metadataFile.addAlternateId(dcsResourceIdentifier);

    File metaFile = new File(filePath);
    metadataFile.setSizeBytes(metaFile.length());

    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Date now = new Date();
    String strDate = sdfDate.format(now);
    metadataFile.setMetadataUpdateDate(strDate);

    researchObject.addFile(metadataFile);

    BatchIndexer<ResearchObject> indexer = new ROBatchIndexer(SeadQueryService.solr, null);
    indexer.add(researchObject);
    indexer.close();

    return "{\n" + "  \"response\" : \"Successfully added object - " + identifier + "\"" + "}";
}