org.jcp.xml.dsig.internal.HmacSHA1 Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal;
/* */
/* */ import java.security.InvalidKeyException;
/* */ import java.security.Key;
/* */ import java.security.MessageDigest;
/* */ import java.security.NoSuchAlgorithmException;
/* */ import java.security.SignatureException;
/* */ import java.util.logging.Level;
/* */ import java.util.logging.Logger;
/* */
/* */ public class HmacSHA1
/* */ {
/* 29 */ private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal");
/* */ private static final int SHA1_BLOCK = 64;
/* */ private byte[] key_opad;
/* 34 */ private boolean initialized = false;
/* */ private Key key;
/* */ private MessageDigest digest;
/* */ private int byte_length;
/* */
/* */ public void init(Key key, int length)
/* */ throws InvalidKeyException
/* */ {
/* 48 */ if (key == null)
/* 49 */ throw new InvalidKeyException("The key should not be null");
/* */ try
/* */ {
/* 52 */ this.digest = MessageDigest.getInstance("SHA1");
/* 53 */ initialize(key);
/* */ }
/* */ catch (NoSuchAlgorithmException nsae)
/* */ {
/* 57 */ throw new InvalidKeyException("SHA1 not supported");
/* */ }
/* 59 */ if (length > 0) {
/* 60 */ this.byte_length = (length / 8);
/* */ }
/* */ else {
/* 63 */ this.byte_length = -1;
/* */ }
/* 65 */ if (log.isLoggable(Level.FINE)) {
/* 66 */ log.log(Level.FINE, "byte_length: " + this.byte_length);
/* */ }
/* 68 */ this.initialized = true;
/* */ }
/* */
/* */ public void update(byte[] data)
/* */ {
/* 77 */ this.digest.update(data);
/* */ }
/* */ public void update(byte data) {
/* 80 */ this.digest.update(data);
/* */ }
/* */ public void update(byte[] data, int offset, int len) {
/* 83 */ this.digest.update(data, offset, len);
/* */ }
/* */
/* */ public byte[] sign()
/* */ throws SignatureException
/* */ {
/* 91 */ if (this.byte_length == 0) {
/* 92 */ throw new SignatureException("length should be -1 or greater than zero, but is " + this.byte_length);
/* */ }
/* */
/* 96 */ byte[] value = this.digest.digest();
/* */
/* 98 */ this.digest.reset();
/* 99 */ this.digest.update(this.key_opad);
/* 100 */ this.digest.update(value);
/* 101 */ byte[] result = this.digest.digest();
/* */
/* 103 */ if ((this.byte_length > 0) && (result.length > this.byte_length)) {
/* 104 */ byte[] truncated = new byte[this.byte_length];
/* 105 */ System.arraycopy(result, 0, truncated, 0, this.byte_length);
/* 106 */ result = truncated;
/* */ }
/* 108 */ return result;
/* */ }
/* */
/* */ public boolean verify(byte[] signature)
/* */ throws SignatureException
/* */ {
/* 117 */ return MessageDigest.isEqual(signature, sign());
/* */ }
/* */
/* */ private void initialize(Key key) {
/* 121 */ byte[] rawKey = key.getEncoded();
/* 122 */ byte[] normalizedKey = new byte[64];
/* 123 */ if (rawKey.length > 64) {
/* 124 */ this.digest.reset();
/* 125 */ rawKey = this.digest.digest(rawKey);
/* */ }
/* 127 */ System.arraycopy(rawKey, 0, normalizedKey, 0, rawKey.length);
/* 128 */ for (int i = rawKey.length; i < 64; i++) {
/* 129 */ normalizedKey[i] = 0;
/* */ }
/* 131 */ byte[] key_ipad = new byte[64];
/* 132 */ this.key_opad = new byte[64];
/* 133 */ for (int i = 0; i < 64; i++) {
/* 134 */ key_ipad[i] = ((byte)(normalizedKey[i] ^ 0x36));
/* 135 */ this.key_opad[i] = ((byte)(normalizedKey[i] ^ 0x5C));
/* */ }
/* */
/* 138 */ this.digest.reset();
/* 139 */ this.digest.update(key_ipad);
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.HmacSHA1
* JD-Core Version: 0.6.2
*/