Ejemplo n.º 1
0
 @JRubyMethod(name = "verify")
 public IRubyObject verify(IRubyObject digest, IRubyObject sig, IRubyObject data) {
   if (!(digest instanceof Digest)) {
     throw newPKeyError(getRuntime(), "invalid digest");
   }
   if (!(sig instanceof RubyString)) {
     throw newPKeyError(getRuntime(), "invalid signature");
   }
   if (!(data instanceof RubyString)) {
     throw newPKeyError(getRuntime(), "invalid data");
   }
   byte[] sigBytes = ((RubyString) sig).getBytes();
   byte[] dataBytes = ((RubyString) data).getBytes();
   String algorithm = ((Digest) digest).getShortAlgorithm() + "WITH" + getAlgorithm();
   boolean valid;
   try {
     Signature signature = SecurityHelper.getSignature(algorithm);
     signature.initVerify(getPublicKey());
     signature.update(dataBytes);
     valid = signature.verify(sigBytes);
   } catch (NoSuchAlgorithmException e) {
     throw newPKeyError(getRuntime(), "unsupported algorithm: " + algorithm);
   } catch (SignatureException e) {
     throw newPKeyError(getRuntime(), "invalid signature");
   } catch (InvalidKeyException e) {
     throw newPKeyError(getRuntime(), "invalid key");
   }
   return getRuntime().newBoolean(valid);
 }
Ejemplo n.º 2
0
  @JRubyMethod(name = "sign")
  public IRubyObject sign(IRubyObject digest, IRubyObject data) {
    if (!this.callMethod(getRuntime().getCurrentContext(), "private?").isTrue()) {
      throw getRuntime().newArgumentError("Private key is needed.");
    }
    String digAlg = ((Digest) digest).getShortAlgorithm();
    try {
      Signature signature = SecurityHelper.getSignature(digAlg + "WITH" + getAlgorithm());
      signature.initSign(getPrivateKey());
      byte[] inp = data.convertToString().getBytes();
      signature.update(inp);
      byte[] sigge = signature.sign();
      return RubyString.newString(getRuntime(), sigge);
    } catch (GeneralSecurityException gse) {
      throw newPKeyError(getRuntime(), gse.getMessage());
    }
    /*
    GetPKey(self, pkey);
    EVP_SignInit(&ctx, GetDigestPtr(digest));
    StringValue(data);
    EVP_SignUpdate(&ctx, RSTRING(data)->ptr, RSTRING(data)->len);
    str = rb_str_new(0, EVP_PKEY_size(pkey)+16);
    if (!EVP_SignFinal(&ctx, RSTRING(str)->ptr, &buf_len, pkey))
    ossl_raise(ePKeyError, NULL);
    assert(buf_len <= RSTRING(str)->len);
    RSTRING(str)->len = buf_len;
    RSTRING(str)->ptr[buf_len] = 0;

    return str;
         */
  }