1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2005 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 /* 22 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 23 */ 24 /* 25 * $Id: DigesterOutputStream.java,v 1.2 2008/07/24 15:20:31 mullan Exp $ 26 */ 27 package org.jcp.xml.dsig.internal; 28 29 import java.io.ByteArrayInputStream; 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.security.MessageDigest; 33 import java.util.logging.Logger; 34 import java.util.logging.Level; 35 36 import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream; 37 38 /** 39 * This class has been modified slightly to use java.security.MessageDigest 40 * objects as input, rather than 41 * com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects. 42 * It also optionally caches the input bytes. 43 * 44 * @author raul 45 * @author Sean Mullan 46 */ 47 public class DigesterOutputStream extends OutputStream { 48 private boolean buffer = false; 49 private UnsyncByteArrayOutputStream bos; 50 private final MessageDigest md; 51 private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal"); 52 53 /** 54 * Creates a DigesterOutputStream. 55 * 56 * @param md the MessageDigest 57 */ 58 public DigesterOutputStream(MessageDigest md) { 59 this(md, false); 60 } 61 62 /** 63 * Creates a DigesterOutputStream. 64 * 65 * @param md the MessageDigest 66 * @param buffer if true, caches the input bytes 67 */ 68 public DigesterOutputStream(MessageDigest md, boolean buffer) { 69 this.md = md; 70 this.buffer = buffer; 71 if (buffer) { 72 bos = new UnsyncByteArrayOutputStream(); 73 } 74 } 75 76 /** @inheritDoc */ 77 public void write(byte[] input) { 78 write(input, 0, input.length); 79 } 80 81 /** @inheritDoc */ 82 public void write(int input) { 83 if (buffer) { 84 bos.write(input); 85 } 86 md.update((byte)input); 87 } 88 89 /** @inheritDoc */ 90 public void write(byte[] input, int offset, int len) { 91 if (buffer) { 92 bos.write(input, offset, len); 93 } 94 if (log.isLoggable(Level.FINER)) { 95 log.log(Level.FINER, "Pre-digested input:"); 96 StringBuffer sb = new StringBuffer(len); 97 for (int i=offset; i<(offset+len); i++) { 98 sb.append((char) input[i]); 99 } 100 log.log(Level.FINER, sb.toString()); 101 } 102 md.update(input, offset, len); 103 } 104 105 /** 106 * @return the digest value 107 */ 108 public byte[] getDigestValue() { 109 return md.digest(); 110 } 111 112 /** 113 * @return an input stream containing the cached bytes, or 114 * null if not cached 115 */ 116 public InputStream getInputStream() { 117 if (buffer) { 118 return new ByteArrayInputStream(bos.toByteArray()); 119 } else { 120 return null; 121 } 122 } 123 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 /* 24 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 25 */ 26 /* 27 * $Id: DigesterOutputStream.java,v 1.5 2005/12/20 20:02:39 mullan Exp $ 28 */ 29 package org.jcp.xml.dsig.internal; 30 31 import java.io.ByteArrayInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.OutputStream; 35 import java.security.MessageDigest; 36 37 import com.sun.org.apache.xml.internal.security.utils.UnsyncByteArrayOutputStream; 38 39 /** 40 * This class has been modified slightly to use java.security.MessageDigest 41 * objects as input, rather than 42 * com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects. 43 * It also optionally caches the input bytes. 44 * 45 * @author raul 46 * @author Sean Mullan 47 */ 48 public class DigesterOutputStream extends OutputStream { 49 private static java.util.logging.Logger log = 50 java.util.logging.Logger.getLogger("org.jcp.xml.dsig.internal"); 51 52 private final boolean buffer; 53 private UnsyncByteArrayOutputStream bos; 54 private final MessageDigest md; 55 56 /** 57 * Creates a DigesterOutputStream. 58 * 59 * @param md the MessageDigest 60 */ 61 public DigesterOutputStream(MessageDigest md) { 62 this(md, false); 63 } 64 65 /** 66 * Creates a DigesterOutputStream. 67 * 68 * @param md the MessageDigest 69 * @param buffer if true, caches the input bytes 70 */ 71 public DigesterOutputStream(MessageDigest md, boolean buffer) { 72 this.md = md; 73 this.buffer = buffer; 74 if (buffer) { 75 bos = new UnsyncByteArrayOutputStream(); 76 } 77 } 78 79 public void write(int input) { 80 if (buffer) { 81 bos.write(input); 82 } 83 md.update((byte)input); 84 } 85 86 @Override 87 public void write(byte[] input, int offset, int len) { 88 if (buffer) { 89 bos.write(input, offset, len); 90 } 91 if (log.isLoggable(java.util.logging.Level.FINE)) { 92 log.log(java.util.logging.Level.FINE, "Pre-digested input:"); 93 StringBuilder sb = new StringBuilder(len); 94 for (int i = offset; i < (offset + len); i++) { 95 sb.append((char)input[i]); 96 } 97 log.log(java.util.logging.Level.FINE, sb.toString()); 98 } 99 md.update(input, offset, len); 100 } 101 102 /** 103 * @return the digest value 104 */ 105 public byte[] getDigestValue() { 106 return md.digest(); 107 } 108 109 /** 110 * @return an input stream containing the cached bytes, or 111 * null if not cached 112 */ 113 public InputStream getInputStream() { 114 if (buffer) { 115 return new ByteArrayInputStream(bos.toByteArray()); 116 } else { 117 return null; 118 } 119 } 120 121 @Override 122 public void close() throws IOException { 123 if (buffer) { 124 bos.close(); 125 } 126 } 127 } |