/*
* 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.IOException;
import java.io.OutputStream;
import java.io.Serializable;
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.CrypterWriter;
public class SynchronousCrypterWriter implements CrypterWriter<SynchronousCrypter>, Serializable {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
public SynchronousCrypterWriter() {
super();
}
@Override
public void write(final SynchronousCrypter crypter, final OutputStream outputStream) {
Assert.notNull(crypter, "crypter");
Assert.notNull(outputStream, "outputStream");
try {
SecretKey key = crypter.getSecretKey();
byte[] keyBytes = key.getEncoded();
String beginText = String.format(CrypterIOHelper.BEGIN_SECRET_KEY_TEMPLATE, crypter.getAlgorithm());
String endText = String.format(CrypterIOHelper.END_SECREY_KEY_TEMPLATE, crypter.getAlgorithm());
CrypterIOHelper.writeKey(beginText, endText, keyBytes, outputStream);
} catch (IOException e) {
throw new CrypterException(e);
}
}
}