author | ccheung |
Wed, 09 Oct 2019 16:42:18 -0700 | |
changeset 58528 | d0519b8bd8d9 |
parent 50614 | 3810c9a2efa1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
7 |
* or more contributor license agreements. See the NOTICE file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
9 |
* regarding copyright ownership. The ASF licenses this file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
10 |
* to you under the Apache License, Version 2.0 (the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
11 |
* "License"); you may not use this file except in compliance |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
2 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
2 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
16 |
* Unless required by applicable law or agreed to in writing, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
17 |
* software distributed under the License is distributed on an |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
19 |
* KIND, either express or implied. See the License for the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
21 |
* under the License. |
2 | 22 |
*/ |
23 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
24 |
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. |
1337 | 25 |
*/ |
26 |
/* |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
27 |
* $Id: DigesterOutputStream.java, v 1.5 2005/12/20 20:02:39 mullan Exp $ |
2 | 28 |
*/ |
29 |
package org.jcp.xml.dsig.internal; |
|
30 |
||
31 |
import java.io.ByteArrayInputStream; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
32 |
import java.io.IOException; |
2 | 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 |
|
1337 | 42 |
* com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm objects. |
2 | 43 |
* It also optionally caches the input bytes. |
44 |
* |
|
45 |
*/ |
|
46 |
public class DigesterOutputStream extends OutputStream { |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
47 |
private static final com.sun.org.slf4j.internal.Logger LOG = |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
48 |
com.sun.org.slf4j.internal.LoggerFactory.getLogger(DigesterOutputStream.class); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
49 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
50 |
private final boolean buffer; |
2 | 51 |
private UnsyncByteArrayOutputStream bos; |
52 |
private final MessageDigest md; |
|
53 |
||
54 |
/** |
|
55 |
* Creates a DigesterOutputStream. |
|
56 |
* |
|
57 |
* @param md the MessageDigest |
|
58 |
*/ |
|
59 |
public DigesterOutputStream(MessageDigest md) { |
|
60 |
this(md, false); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Creates a DigesterOutputStream. |
|
65 |
* |
|
66 |
* @param md the MessageDigest |
|
67 |
* @param buffer if true, caches the input bytes |
|
68 |
*/ |
|
69 |
public DigesterOutputStream(MessageDigest md, boolean buffer) { |
|
70 |
this.md = md; |
|
71 |
this.buffer = buffer; |
|
72 |
if (buffer) { |
|
73 |
bos = new UnsyncByteArrayOutputStream(); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
public void write(int input) { |
|
78 |
if (buffer) { |
|
79 |
bos.write(input); |
|
80 |
} |
|
81 |
md.update((byte)input); |
|
82 |
} |
|
83 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
84 |
@Override |
2 | 85 |
public void write(byte[] input, int offset, int len) { |
86 |
if (buffer) { |
|
87 |
bos.write(input, offset, len); |
|
88 |
} |
|
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
89 |
if (LOG.isDebugEnabled()) { |
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
90 |
LOG.debug("Pre-digested input:"); |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
91 |
StringBuilder sb = new StringBuilder(len); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
92 |
for (int i = offset; i < (offset + len); i++) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
93 |
sb.append((char)input[i]); |
2 | 94 |
} |
50614
3810c9a2efa1
8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
47216
diff
changeset
|
95 |
LOG.debug(sb.toString()); |
2 | 96 |
} |
97 |
md.update(input, offset, len); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* @return the digest value |
|
102 |
*/ |
|
103 |
public byte[] getDigestValue() { |
|
104 |
return md.digest(); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* @return an input stream containing the cached bytes, or |
|
109 |
* null if not cached |
|
110 |
*/ |
|
111 |
public InputStream getInputStream() { |
|
112 |
if (buffer) { |
|
113 |
return new ByteArrayInputStream(bos.toByteArray()); |
|
114 |
} else { |
|
115 |
return null; |
|
116 |
} |
|
117 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
118 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
119 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
120 |
public void close() throws IOException { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
121 |
if (buffer) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
122 |
bos.close(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
123 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
5506
diff
changeset
|
124 |
} |
2 | 125 |
} |