com.kuzumeji.platform.standard.SecurityServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.kuzumeji.platform.standard.SecurityServiceTest.java

Source

// ----------------------------------------------------------------------------
// Copyright (C) Kuzumeji Evolution Laboratory. All rights reserved.
// GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// http://www.gnu.org/licenses/gpl-3.0-standalone.html
// ----------------------------------------------------------------------------
package com.kuzumeji.platform.standard;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.File;
import java.security.Key;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.MessageFormat;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @see SecurityService
 * @author nilcy
 */
@SuppressWarnings("all")
public class SecurityServiceTest {
    SecurityService testee = new SecurityService();
    private static final Logger LOG = LoggerFactory.getLogger(SecurityServiceTest.class);

    @Test
    public void testKeyPair() {
        // RSA???
        final KeyPair keyPair = testee.generateKeyPair();
        final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        LOG.debug("?->{}", dumpKeyPair(publicKey));
        LOG.debug("?->{}", dumpKeyPair(privateKey));
        // RSA???/
        testee.saveKeyPair("testee", keyPair);
        final KeyPair keyPair2 = testee.loadKeyPair("testee");
        assertThat(keyPair2.getPublic().getAlgorithm(), is(publicKey.getAlgorithm()));
        assertThat(keyPair2.getPublic().getFormat(), is(publicKey.getFormat()));
        assertThat(keyPair2.getPublic().getEncoded(), is(publicKey.getEncoded()));
        assertThat(keyPair2.getPrivate().getAlgorithm(), is(privateKey.getAlgorithm()));
        assertThat(keyPair2.getPrivate().getFormat(), is(privateKey.getFormat()));
        assertThat(keyPair2.getPrivate().getEncoded(), is(privateKey.getEncoded()));
        // ???(??)
        final File file = testee.savePublicKeyFile(publicKey);
        LOG.debug("? : {}", file.getPath());
    }

    @Test
    public void testEncryptDecrypt() {
        // RSA???
        final KeyPair keyPair = testee.generateKeyPair();
        final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        // ???
        final char[] password = "hello, world!!".toCharArray();
        final byte[] salt = new byte[] { 1 };
        final byte[] secretKey = testee.createCommonKey(password, salt);
        LOG.debug("? : {}", Hex.encodeHexString(secretKey));
        // ???
        final byte[] encryptedSecretKey = testee.encrypt(publicKey, secretKey);
        LOG.debug("??? : {}", Hex.encodeHexString(encryptedSecretKey));
        // ???
        final byte[] decryptedSecretKey = testee.decrypt(privateKey, encryptedSecretKey);
        assertThat(decryptedSecretKey, is(secretKey));
        LOG.debug("??? : {}", Hex.encodeHexString(decryptedSecretKey));
        // ???
        final String text = "?????????????";
        LOG.debug(" : {}", text);
        final SecuredData context = testee.encrypt(secretKey, text.getBytes());
        LOG.debug("??={}", Hex.encodeHexString(context.getEncrypted()));
        LOG.debug("???(IV)={}", Hex.encodeHexString(context.getVector()));
        // ???
        final byte[] decryptedMessage = testee.decrypt(secretKey, context);
        assertThat(decryptedMessage, is(text.getBytes()));
        LOG.debug("? : {}", new String(decryptedMessage));
    }

    private static String dumpKeyPair(final Key key) {
        return MessageFormat.format("?:{0} ?:{1} ?:{2}", key.getAlgorithm(), key.getFormat(),
                Hex.encodeHexString(key.getEncoded()));
    }

    @Test
    public void testSignature() {
        final KeyPair keyPair = testee.generateKeyPair();
        final RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        final RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        final byte[] message = "?????????????".getBytes();
        final byte[] signature = testee.signature(keyPair.getPrivate(), message);
        assertThat(testee.verify(keyPair.getPublic(), signature, message), is(true));
    }
}