Author: simonetripodi
Date: Wed Feb 24 18:01:31 2010
New Revision: 915901

URL: http://svn.apache.org/viewvc?rev=915901&view=rev
Log:
first checkin of HmacSha1MethodAlgorithm class

Added:
    
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
   (with props)

Added: 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
URL: 
http://svn.apache.org/viewvc/labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java?rev=915901&view=auto
==============================================================================
--- 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
 (added)
+++ 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
 Wed Feb 24 18:01:31 2010
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.labs.amber.signature.signers.hmac;
+
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.labs.amber.signature.signers.AbstractMethodAlgorithm;
+import org.apache.labs.amber.signature.signers.SignatureException;
+import org.apache.labs.amber.signature.signers.SignatureMethod;
+
+/**
+ * HMAC-SHA1 Method implementation.
+ *
+ * @version $Id$
+ */
+...@signaturemethod("HMAC-SHA1")
+public final class HmacSha1MethodAlgorithm extends 
AbstractMethodAlgorithm<HmacSha1Key, HmacSha1Key> {
+
+    /**
+     * The algorithm name.
+     */
+    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
+
+    /**
+     * {...@inheritdoc}
+     */
+    @Override
+    protected String encode(HmacSha1Key signingKey,
+            String secretCredential,
+            String baseString) throws SignatureException {
+        String key = new StringBuilder(percentEncode(signingKey.getValue()))
+                .append('&')
+                .append(percentEncode(secretCredential))
+                .toString();
+
+        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), 
HMAC_SHA1_ALGORITHM);
+
+        Mac mac = null;
+        try {
+            mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
+        } catch (NoSuchAlgorithmException e) {
+            throw new SignatureException("HMAC-SHA1 Algorithm not supported", 
e);
+        }
+
+        try {
+            mac.init(secretKeySpec);
+        } catch (InvalidKeyException e) {
+            throw new SignatureException(new StringBuilder("Signing key '")
+                    .append(key)
+                    .append("' caused HMAC-SHA1 error")
+                    .toString(), e);
+        }
+
+        byte[] rawHmac = mac.doFinal(baseString.getBytes());
+
+        return Base64.encodeBase64String(rawHmac);
+    }
+
+    /**
+     * {...@inheritdoc}
+     */
+    @Override
+    protected boolean verify(String signature,
+            HmacSha1Key verifyingKey,
+            String secretCredential,
+            String baseString)
+            throws SignatureException {
+        String expectedSignature = this.encode(verifyingKey, secretCredential, 
baseString);
+        return expectedSignature.equals(signature);
+    }
+
+}

Propchange: 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: 
labs/amber/signature-api/src/main/java/org/apache/labs/amber/signature/signers/hmac/HmacSha1MethodAlgorithm.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to