Packaging for Maven

master
Vyacheslav N. Boyko 2017-06-22 23:56:30 +03:00
parent cce41220a0
commit 0cd20664c7
7 changed files with 422 additions and 0 deletions

11
.gitignore vendored 100644
View File

@ -0,0 +1,11 @@
.idea/**
.idea/*
.idea
*.iml
out/**
out/*
lib/**
lib/*
target/**
target/*
build.7z

101
pom.xml 100644
View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.bvn13</groupId>
<artifactId>encryptedpreferences</artifactId>
<version>1.0</version>
<name>EncryptedPreferences</name>
<description>EncryptedPreferences for Java</description>
<url>https://github.com/bvn13/EncryptedPreferences</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>owner</id>
<name>Vyacheslav Boyko</name>
<email>mail4bvn@gmail.com</email>
<timezone>UTC+3</timezone>
</developer>
</developers>
<properties>
<java.version>1.8</java.version>
<github.global.server>github</github.global.server>
<github.maven-plugin>0.12</github.maven-plugin>
<!-- <github.global.oauth2Token>${env.GITHUB_OAUTH_TOKEN}</github.global.oauth2Token> -->
<site.path>https://github.com/bvn13/EncryptedPreferences</site.path>
</properties>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>${github.maven-plugin}</version>
<configuration>
<message>Maven artifacts for ${project.version}</message>
<noJekyll>true</noJekyll>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<branch>refs/heads/mvn-repo</branch>
<includes><include>**/*</include></includes>
<repositoryName>EncryptedPreferences</repositoryName>
<repositoryOwner>bvn13</repositoryOwner>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,91 @@
package ru.bvn13.encryptedpreferences;
import java.util.prefs.*;
/**
* Created by bvn13 on 22.06.2017.
*/
public class DelegatedPreferences extends AbstractPreferences
{
private AbstractPreferences target;
static private final boolean verbose = false;
protected DelegatedPreferences( AbstractPreferences parent, String name,
AbstractPreferences target ) {
super( parent, name );
this.target = target;
}
protected String getSpi( String key ) {
if (verbose) {
System.out.println( "DP["+target+"]:getSpi( "+key+" )" );
}
return target.get( key, null );
}
protected void putSpi( String key, String value ) {
if (verbose) {
System.out.println( "DP["+target+"]:putSpi( "+key+", "+value+" )" );
}
target.put( key, value );
}
protected void removeSpi( String key ) {
if (verbose) {
System.out.println( "DP["+target+"]:removeSpi( "+key+" )" );
}
target.remove( key );
}
protected AbstractPreferences childSpi( String name ) {
if (verbose) {
System.out.println( "DP["+target+"]:chlidSpi( "+name+" )" );
}
return (AbstractPreferences)target.node( name );
}
protected void removeNodeSpi() throws BackingStoreException {
if (verbose) {
System.out.println( "DP["+target+"]:removeNode()" );
}
target.removeNode();
}
protected String[] keysSpi() throws BackingStoreException {
if (verbose) {
System.out.println( "DP["+target+"]:keysSpi()" );
}
return target.keys();
}
protected String[] childrenNamesSpi() throws BackingStoreException {
if (verbose) {
System.out.println( "DP["+target+"]:childrenNamesSpi()" );
}
return target.childrenNames();
}
protected void syncSpi() throws BackingStoreException {
if (verbose) {
System.out.println( "DP["+target+"]:sync()" );
}
target.sync();
}
protected void flushSpi() throws BackingStoreException {
if (verbose) {
System.out.println( "DP["+target+"]:flush()" );
}
target.flush();
}
}

View File

@ -0,0 +1,82 @@
package ru.bvn13.encryptedpreferences;
import java.security.*;
import java.util.prefs.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* Created by bvn13 on 22.06.2017.
*/
public class EncryptedPreferences extends ObfuscatedPreferences
{
private EncryptionStuff stuff;
protected EncryptedPreferences( AbstractPreferences parent, String name,
AbstractPreferences target ) {
super( parent, name, target );
}
private void setStuff( EncryptionStuff stuff ) {
this.stuff = stuff;
}
private EncryptionStuff getStuff() {
return stuff;
}
public String obfuscateString( String string ) {
try {
return getStuff().obfuscateString( string );
} catch( GeneralSecurityException gse ) {
gse.printStackTrace();
}
return null;
}
public String deObfuscateString( String string ) {
try {
return getStuff().deObfuscateString( string );
} catch( GeneralSecurityException gse ) {
gse.printStackTrace();
}
return null;
}
public WrappedPreferences wrapChild( WrappedPreferences parent,
String name,
AbstractPreferences child ) {
EncryptedPreferences ep = new EncryptedPreferences( parent, name, child );
ep.setStuff( stuff );
return ep;
}
static public Preferences userNodeForPackage( Class clasz,
SecretKey secretKey ) {
AbstractPreferences ap =
(AbstractPreferences)Preferences.userNodeForPackage( clasz );
EncryptedPreferences ep = new EncryptedPreferences( null, "", ap );
try {
ep.setStuff( new EncryptionStuff( secretKey ) );
return ep;
} catch( GeneralSecurityException gse ) {
gse.printStackTrace();
}
return null;
}
static public Preferences systemNodeForPackage( Class clasz,
SecretKey secretKey ) {
AbstractPreferences ap =
(AbstractPreferences)Preferences.systemNodeForPackage( clasz );
EncryptedPreferences ep = new EncryptedPreferences( null, "", ap );
try {
ep.setStuff( new EncryptionStuff( secretKey ) );
return ep;
} catch( GeneralSecurityException gse ) {
gse.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,72 @@
package ru.bvn13.encryptedpreferences;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* Created by bvn13 on 22.06.2017.
*/
public class EncryptionStuff
{
static private final String algorithm = "DES";
static private SecureRandom sr = new SecureRandom();
private SecretKey secretKey;
private Cipher cipher;
public EncryptionStuff( SecretKey secretKey ) throws GeneralSecurityException {
this.secretKey = secretKey;
cipher = Cipher.getInstance( algorithm );
}
public String arrayToString( byte raw[] ) {
StringBuffer sb = new StringBuffer();
for (int i=0; i<raw.length; ++i) {
short s = (short)raw[i];
if (s>0)
s += 256;
int hi = s>>4;
int lo = s&0xf;
sb.append( (char)('a'+hi) );
sb.append( (char)('a'+lo) );
}
return sb.toString();
}
public byte[] stringToArray( String string ) {
StringBuffer sb = new StringBuffer( string );
int len = sb.length();
if ((len&1)==1)
throw new RuntimeException( "String must be of even length! "+string );
byte raw[] = new byte[len/2];
int ii=0;
for (int i=0; i<len; i+=2) {
int hic = sb.charAt( i ) - 'a';
int loc = sb.charAt( i+1 ) - 'a';
byte b = (byte)( (hic<<4) | loc );
raw[ii++] = b;
}
return raw;
}
synchronized public String obfuscateString( String string )
throws GeneralSecurityException {
cipher.init( Cipher.ENCRYPT_MODE, secretKey, sr );
byte raw[] = string.getBytes();
byte oraw[] = cipher.doFinal( raw );
String ostring = arrayToString( oraw );
return ostring;
}
synchronized public String deObfuscateString( String string )
throws GeneralSecurityException {
cipher.init( Cipher.DECRYPT_MODE, secretKey, sr );
byte raw[] = stringToArray( string );
byte draw[] = cipher.doFinal( raw );
String dstring = new String( draw );
return dstring;
}
}

View File

@ -0,0 +1,39 @@
package ru.bvn13.encryptedpreferences;
import java.util.prefs.*;
/**
* Created by bvn13 on 22.06.2017.
*/
public abstract class ObfuscatedPreferences extends WrappedPreferences
{
protected ObfuscatedPreferences( AbstractPreferences parent, String name,
AbstractPreferences target ) {
super( parent, name, target );
}
protected String getSpi( String key ) {
return deObfuscateString( super.getSpi( obfuscateString( key ) ) );
}
protected void putSpi( String key, String value ) {
super.putSpi( obfuscateString( key ), obfuscateString( value ) );
}
protected void removeSpi( String key ) {
super.removeSpi( obfuscateString( key ) );
}
protected String[] keysSpi() throws BackingStoreException {
String keys[] = super.keysSpi();
String dkeys[] = (String[])keys.clone();
for (int i=0; i<dkeys.length; ++i) {
dkeys[i] = deObfuscateString( dkeys[i] );
}
return dkeys;
}
abstract public String obfuscateString( String string );
abstract public String deObfuscateString( String string );
}

View File

@ -0,0 +1,26 @@
package ru.bvn13.encryptedpreferences;
import java.util.prefs.*;
/**
* Created by bvn13 on 22.06.2017.
*/
abstract public class WrappedPreferences extends DelegatedPreferences
{
protected WrappedPreferences( AbstractPreferences parent, String name,
AbstractPreferences target ) {
super( parent, name, target );
}
protected AbstractPreferences childSpi( String name ) {
return wrapChild( this, name,
(AbstractPreferences)super.childSpi( name ) );
}
public WrappedPreferences wrapChild( WrappedPreferences parent,
String name,
AbstractPreferences child ) {
throw new UnsupportedOperationException(
"You must override WrappedPreferences.wrapChild()" );
}
}