source: josm/trunk/src/oauth/signpost/signature/HmacSha1MessageSigner.java@ 7691

Last change on this file since 7691 was 4231, checked in by stoecker, 13 years ago

add signpost and metadata extractor code to repository directly

File size: 2.1 KB
Line 
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 */
15package oauth.signpost.signature;
16
17import java.io.UnsupportedEncodingException;
18import java.security.GeneralSecurityException;
19
20import javax.crypto.Mac;
21import javax.crypto.SecretKey;
22import javax.crypto.spec.SecretKeySpec;
23
24import oauth.signpost.OAuth;
25import oauth.signpost.exception.OAuthMessageSignerException;
26import oauth.signpost.http.HttpRequest;
27import oauth.signpost.http.HttpParameters;
28
29@SuppressWarnings("serial")
30public class HmacSha1MessageSigner extends OAuthMessageSigner {
31
32 private static final String MAC_NAME = "HmacSHA1";
33
34 @Override
35 public String getSignatureMethod() {
36 return "HMAC-SHA1";
37 }
38
39 @Override
40 public String sign(HttpRequest request, HttpParameters requestParams)
41 throws OAuthMessageSignerException {
42 try {
43 String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'
44 + OAuth.percentEncode(getTokenSecret());
45 byte[] keyBytes = keyString.getBytes(OAuth.ENCODING);
46
47 SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
48 Mac mac = Mac.getInstance(MAC_NAME);
49 mac.init(key);
50
51 String sbs = new SignatureBaseString(request, requestParams).generate();
52 OAuth.debugOut("SBS", sbs);
53 byte[] text = sbs.getBytes(OAuth.ENCODING);
54
55 return base64Encode(mac.doFinal(text)).trim();
56 } catch (GeneralSecurityException e) {
57 throw new OAuthMessageSignerException(e);
58 } catch (UnsupportedEncodingException e) {
59 throw new OAuthMessageSignerException(e);
60 }
61 }
62}
Note: See TracBrowser for help on using the repository browser.