All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scribe.services.RSASha1SignatureService Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
package org.scribe.services;

import org.scribe.exceptions.*;
import java.security.*;

/**
 * A signature service that uses the RSA-SHA1 algorithm.
 */
public class RSASha1SignatureService implements SignatureService
{
  private static final String METHOD = "RSA-SHA1";
  private static final String RSA_SHA1 = "SHA1withRSA";
  private static final String UTF8 = "UTF-8";

  private PrivateKey privateKey;

  public RSASha1SignatureService(PrivateKey privateKey)
  {
    this.privateKey = privateKey;
  }

  /**
   * {@inheritDoc}
   */
  public String getSignature(String baseString, String apiSecret, String tokenSecret)
  {
    try
    {
      Signature signature = Signature.getInstance(RSA_SHA1);
      signature.initSign(privateKey);
      signature.update(baseString.getBytes(UTF8));
      return bytesToBase64String(signature);
    }
    catch (Exception e)
    {
      throw new OAuthSignatureException(baseString, e);
    }
  }

  private String bytesToBase64String(Signature signature) throws SignatureException
  {
    return Base64Encoder.getInstance().encode(signature.sign());
  }

  /**
   * {@inheritDoc}
   */
  public String getSignatureMethod()
  {
    return METHOD;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy