~s-friedemann/tomdroid/sshfs

« back to all changes in this revision

Viewing changes to lib/signpost/signpost-core/src/main/java/oauth/signpost/signature/HmacSha1MessageSigner.java

  • Committer: Guilherme Salgado
  • Date: 2010-08-31 21:44:24 UTC
  • mfrom: (185.1.51 sync-ui)
  • mto: (185.1.54 sync-ui)
  • mto: This revision was merged to the branch mainline in revision 186.
  • Revision ID: salgado@canonical.com-20100831214424-06b64zz707lwpmcl
mergeĀ lp:~tomdroid-developers/sync-ui

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2009 Matthias Kaeppler
2
 
 *
3
 
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 
 * you may not use this file except in compliance with the License.
5
 
 * You may obtain a copy of the License at
6
 
 *
7
 
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 
 *
9
 
 * Unless required by applicable law or agreed to in writing, software
10
 
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 
 * See the License for the specific language governing permissions and
13
 
 * limitations under the License.
14
 
 */
15
 
package oauth.signpost.signature;
16
 
 
17
 
import java.io.UnsupportedEncodingException;
18
 
import java.security.GeneralSecurityException;
19
 
import java.util.Map;
20
 
 
21
 
import javax.crypto.Mac;
22
 
import javax.crypto.SecretKey;
23
 
import javax.crypto.spec.SecretKeySpec;
24
 
 
25
 
import oauth.signpost.OAuth;
26
 
import oauth.signpost.exception.OAuthMessageSignerException;
27
 
import oauth.signpost.http.HttpRequest;
28
 
 
29
 
@SuppressWarnings("serial")
30
 
public class HmacSha1MessageSigner extends OAuthMessageSigner {
31
 
 
32
 
        private static final String MAC_NAME = "HmacSHA1";
33
 
 
34
 
        @Override
35
 
        public String sign(HttpRequest request, Map<String, String> oauthParameters)
36
 
                        throws OAuthMessageSignerException {
37
 
                try {
38
 
                        String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'
39
 
                                        + OAuth.percentEncode(getTokenSecret());
40
 
                        byte[] keyBytes = keyString.getBytes(OAuth.ENCODING);
41
 
 
42
 
                        SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
43
 
                        Mac mac = Mac.getInstance(MAC_NAME);
44
 
                        mac.init(key);
45
 
 
46
 
                        String sbs = computeSignatureBaseString(request, oauthParameters);
47
 
                        byte[] text = sbs.getBytes(OAuth.ENCODING);
48
 
 
49
 
                        return base64Encode(mac.doFinal(text)).trim();
50
 
                } catch (GeneralSecurityException e) {
51
 
                        throw new OAuthMessageSignerException(e);
52
 
                } catch (UnsupportedEncodingException e) {
53
 
                        throw new OAuthMessageSignerException(e);
54
 
                }
55
 
        }
56
 
}