Package br.net.woodstock.rockframework.security.sign.impl

Source Code of br.net.woodstock.rockframework.security.sign.impl.JCASigner

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.security.sign.impl;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.security.sign.SignatureType;
import br.net.woodstock.rockframework.security.sign.SignerException;

public class JCASigner extends AbstractSigner {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  private PublicKey      publicKey;

  private PrivateKey      privateKey;

  private SignatureType    signType;

  public JCASigner(final PublicKey publicKey, final SignatureType signType) {
    Assert.notNull(publicKey, "publicKey");
    Assert.notNull(signType, "signType");
    this.init(new KeyPair(publicKey, null), signType);
  }

  public JCASigner(final PrivateKey privateKey, final SignatureType signType) {
    Assert.notNull(privateKey, "privateKey");
    Assert.notNull(signType, "signType");
    this.init(new KeyPair(null, privateKey), signType);
  }

  public JCASigner(final Certificate certificate, final SignatureType signType) {
    Assert.notNull(certificate, "certificate");
    Assert.notNull(signType, "signType");
    this.init(new KeyPair(certificate.getPublicKey(), null), signType);
  }

  public JCASigner(final KeyPair keyPair, final SignatureType signType) {
    super();
    Assert.notNull(keyPair, "keyPair");
    Assert.notNull(signType, "signType");
    this.init(keyPair, signType);
  }

  private void init(final KeyPair keyPair, final SignatureType signType) {
    this.publicKey = keyPair.getPublic();
    this.privateKey = keyPair.getPrivate();
    this.signType = signType;
  }

  @Override
  public byte[] sign(final byte[] data) {
    if (this.privateKey == null) {
      throw new SignerException("Private key is null");
    }
    try {
      Signature s = Signature.getInstance(this.signType.getAlgorithm());

      s.initSign(this.privateKey);
      s.update(data);

      byte[] bytes = s.sign();
      return bytes;
    } catch (NoSuchAlgorithmException e) {
      throw new SignerException(e);
    } catch (InvalidKeyException e) {
      throw new SignerException(e);
    } catch (SignatureException e) {
      throw new SignerException(e);
    }
  }

  @Override
  public boolean verify(final byte[] data, final byte[] signature) {
    if (this.publicKey == null) {
      throw new SignerException("Public key is null");
    }
    try {
      Signature s = Signature.getInstance(this.signType.getAlgorithm());

      s.initVerify(this.publicKey);
      s.update(data);

      boolean ok = s.verify(signature);
      return ok;
    } catch (NoSuchAlgorithmException e) {
      throw new SignerException(e);
    } catch (InvalidKeyException e) {
      throw new SignerException(e);
    } catch (SignatureException e) {
      throw new SignerException(e);
    }
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.security.sign.impl.JCASigner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.