Exemplo n.º 1
0
  /**
   * Creates signature for data.
   *
   * @param alg Algorithm.
   * @param prv Provider.
   * @param key Private key.
   * @param data Data.
   * @return Signature.
   * @throws GeneralSecurityException If any exception occurs while signature creation.
   */
  public static byte[] createSignature(String alg, String prv, PrivateKey key, byte[] data)
      throws GeneralSecurityException {
    assert !F.isEmpty(alg);
    assert !F.isEmpty(prv);
    assert key != null;
    assert data != null;

    Signature sign = Signature.getInstance(alg, prv);

    sign.initSign(key);
    sign.update(data);

    return sign.sign();
  }
Exemplo n.º 2
0
  /**
   * Verifies signature for data.
   *
   * @param alg Algorithm.
   * @param prv Provider.
   * @param key Public key.
   * @param data Data.
   * @param sign Signature.
   * @return {@code true} if signature was verified, {@code false} - otherwise.
   * @throws GeneralSecurityException If any exception occurs while verifying.
   */
  public static boolean verifySignature(
      String alg, String prv, PublicKey key, byte[] data, byte[] sign)
      throws GeneralSecurityException {
    assert !F.isEmpty(alg);
    assert !F.isEmpty(prv);
    assert key != null;
    assert data != null;
    assert sign != null;

    Signature sign0 = Signature.getInstance(alg, prv);

    sign0.initVerify(key);
    sign0.update(data);

    return sign0.verify(sign);
  }