2
|
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 |
/*
|
1337
|
22 |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
|
|
23 |
*/
|
|
24 |
/*
|
|
25 |
* $Id: DigesterOutputStream.java,v 1.2 2008/07/24 15:20:31 mullan Exp $
|
2
|
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
|
1337
|
41 |
* com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects.
|
2
|
42 |
* It also optionally caches the input bytes.
|
|
43 |
*
|
|
44 |
* @author raul
|
1337
|
45 |
* @author Sean Mullan
|
2
|
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 |
}
|