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

Source Code of br.net.woodstock.rockframework.security.crypt.impl.SynchronousCrypterReader

/*
* 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.crypt.impl;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Scanner;

import javax.crypto.SecretKey;

import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.security.crypt.CrypterException;
import br.net.woodstock.rockframework.security.crypt.CrypterReader;
import br.net.woodstock.rockframework.security.crypt.KeyType;
import br.net.woodstock.rockframework.security.crypt.util.Keys;

public class SynchronousCrypterReader implements CrypterReader<SynchronousCrypter>, Serializable {

  private static final long  serialVersionUID  = RockFrameworkVersion.VERSION;

  public SynchronousCrypterReader() {
    super();
  }

  @Override
  public SynchronousCrypter read(final InputStream inputStream) {
    Assert.notNull(inputStream, "inputStream");
    try {
      Scanner scanner = new Scanner(inputStream);
      StringBuilder keyText = new StringBuilder();
      String algorithm = null;
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (!line.startsWith(CrypterIOHelper.SEPARATOR)) {
          keyText.append(line);
          keyText.append(CrypterIOHelper.NEW_LINE);
        } else if (algorithm == null) {
          algorithm = CrypterIOHelper.getAlgorithm(line);
        }
      }
      scanner.close();
     
      if (algorithm == null) {
        throw new IllegalStateException("Could not read secret key algorithm");
      }

      if (keyText.length() == 0) {
        throw new IllegalStateException("Could not read secret key");
      }

      byte[] keyBytes = CrypterIOHelper.readKey(keyText);

      SecretKey secretKey = Keys.getSecretKeyFromFile(keyBytes, KeyType.getKeyType(algorithm));
      SynchronousCrypter crypter = new SynchronousCrypter(secretKey);

      return crypter;
    } catch (Exception e) {
      throw new CrypterException(e);
    }
  }
}
TOP

Related Classes of br.net.woodstock.rockframework.security.crypt.impl.SynchronousCrypterReader

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.