1
/* Copyright (c) 2009 Matthias Kaeppler
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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
15
package oauth.signpost.signature;
17
import java.io.UnsupportedEncodingException;
18
import java.security.GeneralSecurityException;
21
import javax.crypto.Mac;
22
import javax.crypto.SecretKey;
23
import javax.crypto.spec.SecretKeySpec;
25
import oauth.signpost.OAuth;
26
import oauth.signpost.exception.OAuthMessageSignerException;
27
import oauth.signpost.http.HttpRequest;
29
@SuppressWarnings("serial")
30
public class HmacSha1MessageSigner extends OAuthMessageSigner {
32
private static final String MAC_NAME = "HmacSHA1";
35
public String sign(HttpRequest request, Map<String, String> oauthParameters)
36
throws OAuthMessageSignerException {
38
String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'
39
+ OAuth.percentEncode(getTokenSecret());
40
byte[] keyBytes = keyString.getBytes(OAuth.ENCODING);
42
SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
43
Mac mac = Mac.getInstance(MAC_NAME);
46
String sbs = computeSignatureBaseString(request, oauthParameters);
47
byte[] text = sbs.getBytes(OAuth.ENCODING);
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);