package com.jivesoftware.os.routing.bird.deployable;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import oauth.signpost.OAuth;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.http.HttpParameters;
import oauth.signpost.http.HttpRequest;
import oauth.signpost.signature.OAuthMessageSigner;
import oauth.signpost.signature.SignatureBaseString;
import org.glassfish.jersey.oauth1.signature.Base64;

/**
 *
 */
public class RsaSha1MessageSigner extends OAuthMessageSigner {

    private static final String NAME = "RSA-SHA1";

    private static final String SIGNATURE_ALGORITHM = "SHA1withRSA";

    private static final String KEY_TYPE = "RSA";

    @Override
    public String getSignatureMethod() {
        return NAME;
    }

    @Override
    public String sign(HttpRequest request, HttpParameters requestParams)
        throws OAuthMessageSignerException {
        try {
            String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'
                + OAuth.percentEncode(getTokenSecret());
            byte[] keyBytes = keyString.getBytes(OAuth.ENCODING);

            String sbs = new SignatureBaseString(request, requestParams).generate();
            OAuth.debugOut("SBS", sbs);

            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_TYPE);
            EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            signature.initSign(privateKey);
            signature.update(sbs.getBytes());
            byte[] rsasha1 = signature.sign();
            return base64Encode(rsasha1).trim();
        } catch (GeneralSecurityException e) {
            throw new OAuthMessageSignerException(e);
        } catch (UnsupportedEncodingException e) {
            throw new OAuthMessageSignerException(e);
        }
    }
}

@@ -62,35 +62,18 @@
 java.io.
-UnsupportedEncoding
+IO
 Exceptio
@@ -96,30 +96,25 @@
 ecurity.
-GeneralSecurit
+InvalidKe
 yExcepti
@@ -150,16 +150,63 @@
 actory;%0A
+import java.security.NoSuchAlgorithmException;%0A
 import j
@@ -262,16 +262,57 @@
 nature;%0A
+import java.security.SignatureException;%0A
 import j
@@ -375,152 +375,78 @@
 pec.
-PKCS8EncodedKeySpec;%0Aimport javax.crypto.Mac;%0Aimport javax.crypto.SecretKey;%0Aimport javax.crypto.spec.SecretKeySpec;%0Aimport oauth.signpost.OAuth
+InvalidKeySpecException;%0Aimport java.security.spec.PKCS8EncodedKeySpec
 ;%0Aim
@@ -1164,24 +1164,16 @@
 tParams)
-%0A       
  throws 
@@ -1214,239 +1214,146 @@
     
-try %7B%0A            String keyString = OAuth.percentEncode(getConsumerSecret()) + '&'%0A                + OAuth.percentEncode(getTokenSecret());%0A            byte%5B%5D keyBytes = keyString.getBytes(OAuth.ENCODING);%0A%0A            
+byte%5B%5D decodedPrivateKey;%0A        try %7B%0A            decodedPrivateKey = Base64.decode(getConsumerSecret());%0A            String base
 String 
-sbs 
 = ne
@@ -1414,49 +1414,8 @@
 ();%0A
-            OAuth.debugOut(%22SBS%22, sbs);%0A%0A
     
@@ -1623,16 +1623,25 @@
 pec(
-keyBytes
+decodedPrivateKey
 );%0A 
@@ -1789,11 +1789,18 @@
 ate(
-sbs
+baseString
 .get
@@ -1879,15 +1879,16 @@
 urn 
-b
+B
 ase64
-E
+.e
 ncod
@@ -1901,15 +1901,8 @@
 ha1)
-.trim()
 ;%0A  
@@ -1920,23 +1920,37 @@
 ch (
-GeneralSecurity
+IOException %7C NoSuchAlgorithm
 Exce
@@ -1959,103 +1959,67 @@
 ion 
-e) %7B%0A            throw new OAuthMessageSignerException(e);%0A        %7D catch (UnsupportedEncoding
+%7C InvalidKeyException %7C InvalidKeySpecException %7C Signature
 Exce

fix rsa key format