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 |
|
---|
20 | import javax.crypto.Mac;
|
---|
21 | import javax.crypto.SecretKey;
|
---|
22 | import javax.crypto.spec.SecretKeySpec;
|
---|
23 |
|
---|
24 | import oauth.signpost.OAuth;
|
---|
25 | import oauth.signpost.exception.OAuthMessageSignerException;
|
---|
26 | import oauth.signpost.http.HttpRequest;
|
---|
27 | import oauth.signpost.http.HttpParameters;
|
---|
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 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 | }
|
---|