-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TAP5-2008: Implement HMAC signatures on object streams stored on the …
…client
- Loading branch information
Howard M. Lewis Ship
committed
Oct 4, 2012
1 parent
acfdee7
commit 5ad5257
Showing
9 changed files
with
378 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tapestry-core/src/main/java/org/apache/tapestry5/internal/util/MacOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2012 The Apache Software Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.apache.tapestry5.internal.util; | ||
|
||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.tapestry5.ioc.internal.util.InternalUtils; | ||
|
||
import javax.crypto.Mac; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.security.Key; | ||
|
||
/** | ||
* An output stream that wraps around a {@link Mac} (message authentication code algorithm). This is currently | ||
* used for symmetric (private) keys, but in theory could be used with assymetric (public/private) keys. | ||
* | ||
* @since 5.3.6 | ||
*/ | ||
public class MacOutputStream extends OutputStream | ||
{ | ||
private final Mac mac; | ||
|
||
public static MacOutputStream streamFor(Key key) throws IOException | ||
{ | ||
try | ||
{ | ||
Mac mac = Mac.getInstance(key.getAlgorithm()); | ||
mac.init(key); | ||
|
||
return new MacOutputStream(mac); | ||
} catch (Exception ex) | ||
{ | ||
throw new IOException("Unable to create MacOutputStream: " + InternalUtils.toMessage(ex), ex); | ||
} | ||
} | ||
|
||
public MacOutputStream(Mac mac) | ||
{ | ||
assert mac != null; | ||
|
||
this.mac = mac; | ||
} | ||
|
||
/** | ||
* Should only be invoked once, immediately after this stream is closed; it generates the final MAC result, and | ||
* returns it as a Base64 encoded string. | ||
* | ||
* @return Base64 encoded MAC result | ||
*/ | ||
public String getResult() | ||
{ | ||
byte[] result = mac.doFinal(); | ||
|
||
return new String(Base64.encodeBase64(result)); | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException | ||
{ | ||
mac.update((byte) b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) throws IOException | ||
{ | ||
mac.update(b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException | ||
{ | ||
mac.update(b, off, len); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
tapestry-core/src/main/java/org/apache/tapestry5/internal/util/TeeOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2012 The Apache Software Foundation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.apache.tapestry5.internal.util; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** | ||
* An output stream that acts like a "tee", copying all provided bytes to two output streams. This is used, for example, | ||
* to accumulate a hash of content even as it is being written. | ||
* | ||
* @since 5.3.5 | ||
*/ | ||
public class TeeOutputStream extends OutputStream | ||
{ | ||
private final OutputStream left, right; | ||
|
||
public TeeOutputStream(OutputStream left, OutputStream right) | ||
{ | ||
assert left != null; | ||
assert right != null; | ||
|
||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException | ||
{ | ||
left.write(b); | ||
right.write(b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) throws IOException | ||
{ | ||
left.write(b); | ||
right.write(b); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException | ||
{ | ||
left.write(b, off, len); | ||
right.write(b, off, len); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException | ||
{ | ||
left.flush(); | ||
right.flush(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
left.close(); | ||
right.close(); | ||
} | ||
} |
Oops, something went wrong.