/*
* 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 javax.crypto.spec.SecretKeySpec;
import br.net.woodstock.rockframework.security.crypt.CrypterException;
import br.net.woodstock.rockframework.security.crypt.CrypterReader;
import br.net.woodstock.rockframework.util.Assert;
import br.net.woodstock.rockframework.xml.dom.XmlDocument;
import br.net.woodstock.rockframework.xml.dom.XmlElement;
public class SyncCrypterReader implements CrypterReader<SyncCrypter> {
public SyncCrypterReader() {
super();
}
@Override
public SyncCrypter read(final InputStream inputStream) {
Assert.notNull(inputStream, "inputStream");
try {
XmlDocument document = XmlDocument.read(inputStream);
XmlElement root = document.getRoot();
String algorithm = CrypterIOHelper.getElementData(root, CrypterIOHelper.KEY_ALGORITHM_ELEMENT);
byte[] bytes = CrypterIOHelper.getKey(root, CrypterIOHelper.SECRET_KEY_ELEMENT);
SecretKeySpec keySpec = new SecretKeySpec(bytes, algorithm);
SyncCrypter crypter = new SyncCrypter(keySpec);
return crypter;
} catch (Exception e) {
throw new CrypterException(e);
}
}
}