Java 类java.security.DigestOutputStream 实例源码

项目:Mastering-Java-EE-Development-with-WildFly    文件:ContentMD5Writer.java   
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {

    MessageDigest digest = null;
    try {
        digest = getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    DigestOutputStream digestStream = new DigestOutputStream(buffer, digest);

    OutputStream old = context.getOutputStream();
    context.setOutputStream(digestStream);
    try {
        context.proceed();

        byte[] hash = digest.digest();
        String encodedHash = getEncoder().encodeToString(hash);
        context.getHeaders().putSingle(CONTENT_MD5_STRING, encodedHash);
        byte[] content = buffer.toByteArray();
        old.write(content);
    } finally {
        context.setOutputStream(old);
    }
}
项目:keepass2android    文件:PwDatabaseV3.java   
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {

        // Write checksum Checksum
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("SHA-256 not implemented here.");
        }
        NullOutputStream nos = new NullOutputStream();
        DigestOutputStream dos = new DigestOutputStream(nos, md);

        byte[] transformedMasterKey = transformMasterKey(masterSeed2, masterKey, numRounds); 
        dos.write(masterSeed);
        dos.write(transformedMasterKey);

        finalKey = md.digest();
    }
项目:creacoinj    文件:BuildCheckpoints.java   
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
    final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
    MessageDigest digest = Sha256Hash.newDigest();
    final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
    digestOutputStream.on(false);
    final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
    dataOutputStream.writeBytes("CHECKPOINTS 1");
    dataOutputStream.writeInt(0);  // Number of signatures to read. Do this later.
    digestOutputStream.on(true);
    dataOutputStream.writeInt(checkpoints.size());
    ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
    for (StoredBlock block : checkpoints.values()) {
        block.serializeCompact(buffer);
        dataOutputStream.write(buffer.array());
        buffer.position(0);
    }
    dataOutputStream.close();
    Sha256Hash checkpointsHash = Sha256Hash.wrap(digest.digest());
    System.out.println("Hash of checkpoints data is " + checkpointsHash);
    digestOutputStream.close();
    fileOutputStream.close();
    System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
项目:iosched-reader    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:hadoop    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:hadoop    文件:TestJets3tNativeFileSystemStore.java   
protected void writeRenameReadCompare(Path path, long len)
    throws IOException, NoSuchAlgorithmException {
  // If len > fs.s3n.multipart.uploads.block.size,
  // we'll use a multipart upload copy
  MessageDigest digest = MessageDigest.getInstance("MD5");
  OutputStream out = new BufferedOutputStream(
      new DigestOutputStream(fs.create(path, false), digest));
  for (long i = 0; i < len; i++) {
    out.write('Q');
  }
  out.flush();
  out.close();

  assertTrue("Exists", fs.exists(path));

  // Depending on if this file is over 5 GB or not,
  // rename will cause a multipart upload copy
  Path copyPath = path.suffix(".copy");
  fs.rename(path, copyPath);

  assertTrue("Copy exists", fs.exists(copyPath));

  // Download file from S3 and compare the digest against the original
  MessageDigest digest2 = MessageDigest.getInstance("MD5");
  InputStream in = new BufferedInputStream(
      new DigestInputStream(fs.open(copyPath), digest2));
  long copyLen = 0;
  while (in.read() != -1) {copyLen++;}
  in.close();

  assertEquals("Copy length matches original", len, copyLen);
  assertArrayEquals("Digests match", digest.digest(), digest2.digest());
}
项目:jdk8u-jdk    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:openjdk-jdk10    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:openjdk9    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:KeePass2Android    文件:PwDatabaseV3.java   
public void makeFinalKey(byte[] masterSeed, byte[] masterSeed2, int numRounds) throws IOException {

        // Write checksum Checksum
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("SHA-256 not implemented here.");
        }
        NullOutputStream nos = new NullOutputStream();
        DigestOutputStream dos = new DigestOutputStream(nos, md);

        byte[] transformedMasterKey = transformMasterKey(masterSeed2, masterKey, numRounds); 
        dos.write(masterSeed);
        dos.write(transformedMasterKey);

        finalKey = md.digest();
    }
项目:aliyun-oss-hadoop-fs    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:aem-maven-repository    文件:MavenRepositoryServlet.java   
private byte[] generateSha1Hash(Bundle associatedBundle) throws IOException, NoSuchAlgorithmException {
    MessageDigest hash = MessageDigest.getInstance("SHA1");
    hash.reset();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream output = new DigestOutputStream(byteArrayOutputStream, hash);

    writeBundleArtifactFile(output, associatedBundle);

    DigestOutputStream digestOutput = (DigestOutputStream) output;
    byte[] digest = digestOutput.getMessageDigest().digest();
    String hexStr = "";
    for (int i = 0; i < digest.length; i++) {
        hexStr += Integer.toString((digest[i] & 0xff) + 0x100, 16)
                .substring(1);
    }

    return hexStr.getBytes();
}
项目:smconf-android    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:javacc    文件:OutputFile.java   
/**
 * Return a PrintWriter object that may be used to write to this file. Any
 * necessary header information is written by this method.
 *
 * @return
 * @throws IOException
 */
public PrintWriter getPrintWriter() throws IOException {
  if (pw == null) {
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      throw (IOException) (new IOException("No MD5 implementation")
      .initCause(e));
    }
    dos = new DigestOutputStream(new BufferedOutputStream(
        new FileOutputStream(file)), digest);
    pw = new TrapClosePrintWriter(dos);

    // Write the headers....
    String version = compatibleVersion == null ? Version.versionNumber : compatibleVersion;
    pw.println("/* "
        + JavaCCGlobals.getIdString(toolName, file.getName())
        + " Version " + version + " */");
    if (options != null) {
      pw.println("/* JavaCCOptions:" + Options.getOptionsString(options) + " */");
    }
  }

  return pw;
}
项目:big-c    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:jasperreports    文件:AbstractTest.java   
protected String xmlExportDigest(JasperPrint print) 
        throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
    File outputFile = createXmlOutputFile();
    log.debug("XML export output at " + outputFile.getAbsolutePath());

    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    FileOutputStream output = new FileOutputStream(outputFile);
    try
    {
        DigestOutputStream out = new DigestOutputStream(output, digest);
        xmlExport(print, out);
    }
    finally
    {
        output.close();
    }

    return toDigestString(digest);
}
项目:jasperreports    文件:AbstractTest.java   
protected String errExportDigest(Throwable t) 
        throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
    File outputFile = createXmlOutputFile();
    log.debug("Error stack trace at " + outputFile.getAbsolutePath());

    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    FileOutputStream output = new FileOutputStream(outputFile);
    OutputStreamWriter osw = null;
    try
    {
        DigestOutputStream out = new DigestOutputStream(output, digest);
        //PrintStream ps = new PrintStream(out);
        //t.printStackTrace(ps);
        osw = new OutputStreamWriter(out, "UTF-8");
        osw.write(t.getMessage());
    }
    finally
    {
        osw.close();
        output.close();
    }

    return toDigestString(digest);
}
项目:jasperreports    文件:Report.java   
protected String xmlDigest(JasperPrint print) 
        throws NoSuchAlgorithmException, FileNotFoundException, JRException, IOException
{
    File outputFile = createXmlOutputFile();
    log.debug("XML export output at " + outputFile.getAbsolutePath());

    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    FileOutputStream output = new FileOutputStream(outputFile);
    try
    {
        DigestOutputStream out = new DigestOutputStream(output, digest);
        xmlExport(print, out);
    }
    finally
    {
        output.close();
    }

    return toDigestString(digest);
}
项目:jdk8u_jdk    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:lookaside_java-1.8.0-openjdk    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:hawkbit-extensions    文件:MongoDBArtifactStore.java   
private static String computeSHA1Hash(final InputStream stream, final OutputStream os, final String providedSHA1Sum)
        throws NoSuchAlgorithmException, IOException {
    String sha1Hash;
    // compute digest
    // Exception squid:S2070 - not used for hashing sensitive
    // data
    @SuppressWarnings("squid:S2070")
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    try (final DigestOutputStream dos = new DigestOutputStream(os, md)) {
        ByteStreams.copy(stream, dos);
    }
    sha1Hash = BaseEncoding.base16().lowerCase().encode(md.digest());
    if (providedSHA1Sum != null && !providedSHA1Sum.equalsIgnoreCase(sha1Hash)) {
        throw new HashNotMatchException(
                "The given sha1 hash " + providedSHA1Sum + " not matching the calculated sha1 hash " + sha1Hash,
                HashNotMatchException.SHA1);
    }
    return sha1Hash;
}
项目:2015-Google-I-O-app    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:wildfly-swarm    文件:SwarmContentRepository.java   
@Override
public byte[] addContent(InputStream stream) throws IOException {
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        byte[] sha1Bytes;
        Path tmp = File.createTempFile("content", ".tmp").toPath();
        try (OutputStream fos = Files.newOutputStream(tmp)) {
            messageDigest.reset();
            DigestOutputStream dos = new DigestOutputStream(fos, messageDigest);
            BufferedInputStream bis = new BufferedInputStream(stream);
            byte[] bytes = new byte[8192];
            int read;
            while ((read = bis.read(bytes)) > -1) {
                dos.write(bytes, 0, read);
            }
            fos.flush();
            sha1Bytes = messageDigest.digest();
        }
        String key = toKey(sha1Bytes);
        this.index.put(key, tmp.toUri());
        return sha1Bytes;
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    }
}
项目:hadoop-2.6.0-cdh5.4.3    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:hadoop-EAR    文件:TestJournalNodeImageManifest.java   
/**
 * Generate random contents for the image and store it together with the md5
 * for later comparison.
 */
private ContentBody genContent(long txid) throws IOException {
  MessageDigest digester = MD5Hash.getDigester();

  // write through digester so we can roll the written image
  ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
  DigestOutputStream ds = new DigestOutputStream(bos, digester);

  // generate random bytes
  new Random().nextBytes(randomBytes);
  ds.write(randomBytes);
  ds.flush();

  // get written hash
  MD5Hash hash = new MD5Hash(digester.digest());

  // store contents and digest
  digests.put(txid, hash);
  content.put(txid, Arrays.copyOf(randomBytes, randomBytes.length));

  return new ByteArrayBody(bos.toByteArray(), "filename");
}
项目:hadoop-EAR    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:hadoop-EAR    文件:UploadImageServlet.java   
private static synchronized SessionDescriptor startImageUpload(
    UploadImageParam params, ServletContext context) throws IOException {

  // get and validate storage
  Journal journal = getStorage(context, params);
  JNStorage storage = journal.getImageStorage();

  // get tmp image file
  File outputFile = storage.getCheckpointImageFile(params.txId);

  // starting a new upload
  long sessionId = sessionIds.incrementAndGet();

  MessageDigest digester = MD5Hash.getDigester();
  // open the stream that will be used throughout the upload
  FileOutputStream fos = new FileOutputStream(outputFile);
  OutputStream os = new BufferedOutputStream(new DigestOutputStream(fos,
      digester));

  SessionDescriptor sd = new SessionDescriptor(journal, params.journalId,
      sessionId, os, params.txId, digester);
  sessions.put(sessionId, sd);
  InjectionHandler.processEventIO(InjectionEvent.UPLOADIMAGESERVLET_START,
      context);
  return sd;
}
项目:namecoinj    文件:BuildCheckpoints.java   
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
    final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
    digestOutputStream.on(false);
    final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
    dataOutputStream.writeBytes("CHECKPOINTS 1");
    dataOutputStream.writeInt(0);  // Number of signatures to read. Do this later.
    digestOutputStream.on(true);
    dataOutputStream.writeInt(checkpoints.size());
    ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
    for (StoredBlock block : checkpoints.values()) {
        block.serializeCompact(buffer);
        dataOutputStream.write(buffer.array());
        buffer.position(0);
    }
    dataOutputStream.close();
    Sha256Hash checkpointsHash = new Sha256Hash(digest.digest());
    System.out.println("Hash of checkpoints data is " + checkpointsHash);
    digestOutputStream.close();
    fileOutputStream.close();
    System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
项目:hadoop-plus    文件:NativeS3FileSystem.java   
public NativeS3FsOutputStream(Configuration conf,
    NativeFileSystemStore store, String key, Progressable progress,
    int bufferSize) throws IOException {
  this.conf = conf;
  this.key = key;
  this.backupFile = newBackupFile();
  LOG.info("OutputStream for key '" + key + "' writing to tempfile '" + this.backupFile + "'");
  try {
    this.digest = MessageDigest.getInstance("MD5");
    this.backupStream = new BufferedOutputStream(new DigestOutputStream(
        new FileOutputStream(backupFile), this.digest));
  } catch (NoSuchAlgorithmException e) {
    LOG.warn("Cannot load MD5 digest algorithm," +
        "skipping message integrity check.", e);
    this.backupStream = new BufferedOutputStream(
        new FileOutputStream(backupFile));
  }
}
项目:MyJohnDeereAPI-OAuth-Java-Client    文件:Download.java   
public void downloadFileContentsAndComputeMd5() throws NoSuchAlgorithmException, IOException {

        final RestRequest fileDetails = oauthRequestTo(firstFileSelfUri)
                .method("GET")
                .addHeader(new HttpHeader("Accept", "application/octet-stream"))
                .build();

        final RestResponse fileDetailsResponse = fileDetails.fetchResponse();

        checkFilenameInContentDispositionHeader(fileDetailsResponse);

        java.io.File file = new java.io.File("src/main/resources/" + firstFileDetails.getName());
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        final DigestOutputStream byteDigest = new DigestOutputStream(fileOutputStream, getInstance("md5"));

        copy(fileDetailsResponse.getBody(), byteDigest);

        md5FromSinglePieceDownload = byteDigest.getMessageDigest().digest();
    }
项目:MyJohnDeereAPI-OAuth-Java-Client    文件:Download.java   
public void downloadFileInPiecesAndComputeMd5() throws NoSuchAlgorithmException, IOException {
    // Download the file in chunk of 16 MB
    int maxFileSize = 16 * 1024 * 1024;
    int chunkSize = Math.min(maxFileSize, actualFileSize);
    java.io.File file = new java.io.File("src/main/resources/" + firstFileDetails.getName());
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    final DigestOutputStream byteDigest = new DigestOutputStream(fileOutputStream, getInstance("md5"));

    getChunkFromStartAndRecurse(0, chunkSize, actualFileSize, byteDigest);

    System.out.println("File Name = " + firstFileDetails.getName());
    System.out.println("File Size(KB) = " + file.length() / 1024.0);
}
项目:saarang-iosched    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:AppDevFestSudeste2015    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:JJCamera    文件:CloudFileManager.java   
static public byte[] calulateHash(JsonElement contents) {
  MessageDigest md;
  try {
    md = MessageDigest.getInstance("MD5");
  } catch (NoSuchAlgorithmException e) {
    throw new InternalError("MD5 MessageDigest is not available");
  }
  OutputStream byteSink = new OutputStream() {
    @Override
    public void write(int b) throws IOException {
      // ignore, since this is only used to calculate MD5
    }
  };
  DigestOutputStream dis = new DigestOutputStream(byteSink, md);
  new Gson().toJson(contents, new OutputStreamWriter(dis, Charset.forName(DEFAULT_CHARSET_NAME)));
  return dis.getMessageDigest().digest();
}
项目:infobip-open-jdk-8    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:jdk8u-dev-jdk    文件:CipherStreamClose.java   
public static byte[] streamEncrypt(String message, SecretKey key,
    MessageDigest digest)
    throws Exception {

    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }

    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
项目:digibytej-alice    文件:BuildCheckpoints.java   
private static void writeBinaryCheckpoints(TreeMap<Integer, StoredBlock> checkpoints, File file) throws Exception {
    final FileOutputStream fileOutputStream = new FileOutputStream(file, false);
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    final DigestOutputStream digestOutputStream = new DigestOutputStream(fileOutputStream, digest);
    digestOutputStream.on(false);
    final DataOutputStream dataOutputStream = new DataOutputStream(digestOutputStream);
    dataOutputStream.writeBytes("CHECKPOINTS 1");
    dataOutputStream.writeInt(0);  // Number of signatures to read. Do this later.
    digestOutputStream.on(true);
    dataOutputStream.writeInt(checkpoints.size());
    ByteBuffer buffer = ByteBuffer.allocate(StoredBlock.COMPACT_SERIALIZED_SIZE);
    for (StoredBlock block : checkpoints.values()) {
        block.serializeCompact(buffer);
        dataOutputStream.write(buffer.array());
        buffer.position(0);
    }
    dataOutputStream.close();
    Sha256Hash checkpointsHash = new Sha256Hash(digest.digest());
    System.out.println("Hash of checkpoints data is " + checkpointsHash);
    digestOutputStream.close();
    fileOutputStream.close();
    System.out.println("Checkpoints written to '" + file.getCanonicalPath() + "'.");
}
项目:In-the-Box-Fork    文件:DigestOutputStreamTest.java   
/**
 * java.security.DigestOutputStream#setMessageDigest(MessageDigest)
 */
public void test_setMessageDigestLjava_security_MessageDigest() {

    MessageDigest digest = new MyMessageDigest1();
    OutputStream out = new MyOutputStream();

    DigestOutputStream dos = new DigestOutputStream(out, null);

    // non-null parameter
    dos.setMessageDigest(digest);
    assertSame(digest, dos.getMessageDigest());

    // null parameter
    dos.setMessageDigest(null);
    assertNull("getMessageDigest should have returned null", dos.getMessageDigest());
}
项目:In-the-Box-Fork    文件:DigestOutputStreamTest.java   
/**
 * Test #1 for <code>write(int)</code> method<br>
 *
 * Assertion: writes the byte to the output stream<br>
 * Assertion: updates associated digest<br>
 */
public final void testWriteint01()
    throws IOException {
    for (int k=0; k<algorithmName.length; k++) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
            DigestOutputStream dos = new DigestOutputStream(bos, md);
            for (int i=0; i<MY_MESSAGE_LEN; i++) {
                dos.write(myMessage[i]);
            }
            // check that bytes have been written correctly
            assertTrue("write", Arrays.equals(MDGoldenData.getMessage(), bos.toByteArray()));
            // check that associated digest has been updated properly
            assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
                                               MDGoldenData.getDigest(algorithmName[k])));
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}
项目:In-the-Box-Fork    文件:DigestOutputStreamTest.java   
/**
 * Test #1 for <code>write(byte[],int,int)</code> method<br>
 *
 * Assertion: put bytes into output stream<br>
 *
 * Assertion: updates associated digest<br>
 */
public final void test_write$BII_1() throws IOException {
    for (int k=0; k<algorithmName.length; k++) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MY_MESSAGE_LEN);
            MessageDigest md = MessageDigest.getInstance(algorithmName[k]);
            DigestOutputStream dos = new DigestOutputStream(bos, md);

            // write message at once
            dos.write(myMessage, 0, MY_MESSAGE_LEN);

            // check write
            assertTrue("write", Arrays.equals(myMessage, bos.toByteArray()));
            // check that associated digest has been updated properly
            assertTrue("update", Arrays.equals(dos.getMessageDigest().digest(),
                                               MDGoldenData.getDigest(algorithmName[k])));
            return;
        } catch (NoSuchAlgorithmException e) {
            // allowed failure
        }
    }
    fail(getName() + ": no MessageDigest algorithms available - test not performed");
}