From 061d1efd79dafbb21768fca85d08aec4c805663d Mon Sep 17 00:00:00 2001 From: Peter Verhas Date: Sun, 14 May 2023 17:14:05 +0200 Subject: [PATCH] Java Geci dependency was removed, and instead Jamal dependency added with the 2.1.0-SNAPSHOT release it will not compile on the GitHub site, not relesable version --- .mvn/extensions.jam | 3 +- .mvn/extensions.xml | 6 +- pom.jam | 19 +- pom.xml | 23 ++- src/main/jamal/FeatureMethods.java.jam | 26 +++ src/main/jamal/FeatureTypeEnum.java.jam | 21 +++ src/main/jamal/Network.java.jam | 14 ++ src/main/jamal/getType.java.jam | 15 ++ src/main/jamal/getTypeFeature.java.jam | 21 +++ src/main/jamal/isType.java.jam | 10 ++ src/main/java/javax0/license3j/Feature.java | 170 +++++------------- .../license3j/crypto/LicenseKeyPair.java | 80 +++------ .../javax0/license3j/hardware/Network.java | 70 +++----- 13 files changed, 238 insertions(+), 240 deletions(-) create mode 100644 src/main/jamal/FeatureMethods.java.jam create mode 100644 src/main/jamal/FeatureTypeEnum.java.jam create mode 100644 src/main/jamal/Network.java.jam create mode 100644 src/main/jamal/getType.java.jam create mode 100644 src/main/jamal/getTypeFeature.java.jam create mode 100644 src/main/jamal/isType.java.jam diff --git a/.mvn/extensions.jam b/.mvn/extensions.jam index 23121fd..ef2ffc7 100644 --- a/.mvn/extensions.jam +++ b/.mvn/extensions.jam @@ -1,4 +1,5 @@ -{@import https://raw.githubusercontent.com/verhas/jamal/master/version.jim} +{@comment nosave}\ +{@import https://raw.githubusercontent.com/verhas/jamal/master/version.jim}\ com.javax0.jamal jamal-maven-extension - 1.12.2 + 2.0.2 \ No newline at end of file diff --git a/pom.jam b/pom.jam index 3915301..5fae77b 100755 --- a/pom.jam +++ b/pom.jam @@ -1,11 +1,11 @@ -{#thinXml{@xmlFormat} +{@comment} +{#thinXml project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> modelVersion>4.0.0 groupId>com.javax0.license3j artifactId>license3j version>3.2.0 packaging>jar - name>License3j properties>sourceEncoding>UTF-8 distributionManagement> @@ -55,12 +55,6 @@ project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://mav version>5.8.2 scope>test - dependency> - groupId>com.javax0.geci - artifactId>javageci-testengine - version>1.6.5 - scope>test - profiles>profile> id>release build>plugins>plugin> @@ -89,6 +83,13 @@ project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://mav artifactId>junit-jupiter-engine version>5.7.0 + plugin> + groupId>com.javax0.jamal + artifactId>jamal-maven-plugin + version>2.1.0-SNAPSHOT + executions>execution> + goals>goal>jamal + plugin> groupId>org.apache.maven.plugins artifactId>maven-source-plugin @@ -121,4 +122,4 @@ project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://mav serverId>ossrh nexusUrl>https://oss.sonatype.org/ autoReleaseAfterClose>true -} \ No newline at end of file +}{@xmlFormat} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 017b0d8..c3c6130 100755 --- a/pom.xml +++ b/pom.xml @@ -4,8 +4,7 @@ com.javax0.license3j license3j 3.2.0 - jar - + jar License3j UTF-8 @@ -69,13 +68,6 @@ junit-jupiter-engine 5.8.2 test - - - - com.javax0.geci - javageci-testengine - 1.6.5 - test @@ -127,6 +119,19 @@ + + com.javax0.jamal + jamal-maven-plugin + 2.1.0-SNAPSHOT + + + + jamal + + + + + org.apache.maven.plugins maven-source-plugin diff --git a/src/main/jamal/FeatureMethods.java.jam b/src/main/jamal/FeatureMethods.java.jam new file mode 100644 index 0000000..b303705 --- /dev/null +++ b/src/main/jamal/FeatureMethods.java.jam @@ -0,0 +1,26 @@ +{%!@for [separator="\\s*\n\\s*" skipEmpty] ($type,$vType,$value) in `LOOP` +Binary|byte[]|value +String|String|value.getBytes(StandardCharsets.UTF_8) +Byte|Byte|new byte[]{value} +Short|Short|ByteBuffer.allocate(Short.BYTES).putShort(value).array() +Int|Integer|ByteBuffer.allocate(Integer.BYTES).putInt(value).array() +Long|Long|ByteBuffer.allocate(Long.BYTES).putLong(value).array() +Float|Float|ByteBuffer.allocate(Float.BYTES).putFloat(value).array() +Double|Double|ByteBuffer.allocate(Double.BYTES).putDouble(value).array() +BigInteger|BigInteger|value.toByteArray() +uuid|java.util.UUID|ByteBuffer.allocate(2 * Long.BYTES).putLong(value.getLeastSignificantBits()).putLong(value.getMostSignificantBits()).array() +Date|Date|ByteBuffer.allocate(Long.BYTES).putLong(value.getTime()).array() +`LOOP`= + /** + * Create a new {%@case:decap $type%} feature. + * + * @param name the name of the new feature + * @param value the value for the new feature. {@code null} value will throw an exception + * @return the newly created feature object + *{%@comment%}/ + public static Feature {%@case:decap $type%}Feature(String name, $vType value) { + Objects.requireNonNull(value); + return new Feature(name, Type.{%@case:upper $type%}, $value); + } +%} +{%@java:insert to="../java/javax0/license3j/Feature.java" id="FeatureMethods"%} \ No newline at end of file diff --git a/src/main/jamal/FeatureTypeEnum.java.jam b/src/main/jamal/FeatureTypeEnum.java.jam new file mode 100644 index 0000000..e340cc4 --- /dev/null +++ b/src/main/jamal/FeatureTypeEnum.java.jam @@ -0,0 +1,21 @@ +private enum Type {{%@counter:define id=typeSerial%}\ +{%!@for [separator="\\s*\n\\s*" skipEmpty]($type,$length,$cast,$stringer,$unstringer,$sep) in `LOOP` +binary|VARIABLE_LENGTH|byte[]|ba -> Base64.getEncoder().encodeToString((byte[]) ba)|enc -> Base64.getDecoder().decode(enc)| +string|VARIABLE_LENGTH|||s -> s| +byte|||b -> String.format("0x%02X", (Byte) b)|NumericParser.Byte::parse| +short||||NumericParser.Short::parse| +int|Integer.BYTES|Integer||NumericParser.Int::parse| +long||||NumericParser.Long::parse| +float||||Float::parseFloat| +double||||Double::parseDouble| +bigInteger|VARIABLE_LENGTH|||BigInteger::new| +bigDecimal|VARIABLE_LENGTH|||BigDecimal::new| +date|Long.BYTES||Feature::dateFormat|Feature::dateParse| +UUID|2 * Long.BYTES|java.util.UUID||java.util.UUID::fromString|; +`LOOP`= + {%@case:upper $type%}({%typeSerial%}, {%#if/$length/$length/{%@case:cap $type%}.BYTES%}, + Feature::get{%@case:cap $type%}, + (name, value) -> Create.{%@replace /$type/UUID/uuid%}Feature(name, ({%#if/$cast/$cast/{%@case:cap $type%}%}) value), + {%#if/$stringer/$stringer/Object::toString%}, $unstringer){%@if/$sep/$sep/,%} +%} +{%@java:insert to="../java/javax0/license3j/Feature.java" id="TypesEnum"%} \ No newline at end of file diff --git a/src/main/jamal/Network.java.jam b/src/main/jamal/Network.java.jam new file mode 100644 index 0000000..e03ae6c --- /dev/null +++ b/src/main/jamal/Network.java.jam @@ -0,0 +1,14 @@ + {%!@for [separator="\\s*\n\\s*" skipEmpty]($rettype,$name,$exceptions) in + `LOOP` + String|getName| + byte[]|getHardwareAddress|SocketException + String|getDisplayName| + boolean|isLoopback|SocketException + boolean|isVirtual|SocketException + boolean|isPointToPoint|SocketException + `LOOP`= + public $rettype $name(){%#if/$exceptions/ throws $exceptions/%} { + return ni.$name(); + } + %} +{%@java:insert to="../java/javax0/license3j/hardware/Network.java" id="Network"%} \ No newline at end of file diff --git a/src/main/jamal/getType.java.jam b/src/main/jamal/getType.java.jam new file mode 100644 index 0000000..5cd83d6 --- /dev/null +++ b/src/main/jamal/getType.java.jam @@ -0,0 +1,15 @@ +{%!@for ($Type) in (Public,Private)= /**GENERATED CODE + * Get the byte representation of the {%@case:lower $Type%} key as it is returned + * by the underlying security library. It is NOT the byte array + * that contains the algorithm at the start. It is the key in raw + * format. + * + * @return the key as bytes + *{%@comment%}/ + public byte[] get$Type() { + keyNotNull(pair.get$Type()); + final var key = pair.get$Type(); + return getKeyBytes(key); + } +%}\ +{%@java:insert to="../java/javax0/license3j/crypto/LicenseKeyPair.java" id="getTypes"%} diff --git a/src/main/jamal/getTypeFeature.java.jam b/src/main/jamal/getTypeFeature.java.jam new file mode 100644 index 0000000..dff129a --- /dev/null +++ b/src/main/jamal/getTypeFeature.java.jam @@ -0,0 +1,21 @@ + {%@comment UUID and BigDecimal are handled separately, because they differ significantly%}\ + {%!@for [separator="\\s*\n\\s*" skipEmpty]($Type,$return,$rType) in `LOOP` + Binary|value|byte[] + String|new String(value, StandardCharsets.UTF_8)|String + Byte|value[0]|byte + Short|ByteBuffer.wrap(value).getShort()|short + Int|ByteBuffer.wrap(value).getInt()|int + Long|ByteBuffer.wrap(value).getLong()|long + Float|ByteBuffer.wrap(value).getFloat()|float + Double|ByteBuffer.wrap(value).getDouble()|double + BigInteger|new BigInteger(value)|BigInteger + Date|new Date(ByteBuffer.wrap(value).getLong())|Date + `LOOP`= + public $rType get$Type() { + if (type != Type.{%@case:upper $Type%}) { + throw new IllegalArgumentException("Feature is not {%@case:upper $Type%}"); + } + return $return; + } + %}\ +{%@java:insert to="../java/javax0/license3j/Feature.java" id="getTypes"%}\ \ No newline at end of file diff --git a/src/main/jamal/isType.java.jam b/src/main/jamal/isType.java.jam new file mode 100644 index 0000000..b6130bb --- /dev/null +++ b/src/main/jamal/isType.java.jam @@ -0,0 +1,10 @@ + // {%@rot13 TRARENGRQ PBQR%} +{%!@for $Type in (Binary,String,Byte,Short,Int,Long,Float,Double,BigInteger,BigDecimal,Date,UUID)= + /** + * @return {@code true} if the feature is of type $Type + *{%@ident%}/ + public boolean is$Type() { + return type == Type.{%@case:upper $Type%}; + } +%}\ +{%@java:insert to="../java/javax0/license3j/Feature.java" id="isTypes"%}\ \ No newline at end of file diff --git a/src/main/java/javax0/license3j/Feature.java b/src/main/java/javax0/license3j/Feature.java index 58a1e95..41a9994 100644 --- a/src/main/java/javax0/license3j/Feature.java +++ b/src/main/java/javax0/license3j/Feature.java @@ -8,12 +8,7 @@ import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Base64; -import java.util.Date; -import java.util.Objects; -import java.util.TimeZone; -import java.util.UUID; +import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; @@ -43,12 +38,12 @@ */ public class Feature { private static final String[] DATE_FORMAT = - {"yyyy-MM-dd HH:mm:ss.SSS", - "yyyy-MM-dd HH:mm:ss", - "yyyy-MM-dd HH:mm", - "yyyy-MM-dd HH", - "yyyy-MM-dd" - }; + {"yyyy-MM-dd HH:mm:ss.SSS", + "yyyy-MM-dd HH:mm:ss", + "yyyy-MM-dd HH:mm", + "yyyy-MM-dd HH", + "yyyy-MM-dd" + }; private static final int VARIABLE_LENGTH = -1; private final String name; private final Type type; @@ -189,28 +184,16 @@ public byte[] serialized() { final var nameLength = Integer.BYTES + nameBuffer.length; final var valueLength = type.fixedSize == VARIABLE_LENGTH ? Integer.BYTES + value.length : type.fixedSize; final var buffer = ByteBuffer.allocate(typeLength + nameLength + valueLength) - .putInt(type.serialized) - .putInt(nameBuffer.length); + .putInt(type.serialized) + .putInt(nameBuffer.length); if (type.fixedSize == VARIABLE_LENGTH) { buffer.putInt(value.length); } buffer.put(nameBuffer).put(value); return buffer.array(); } - - /*!jamal - // - {%!@for $Type in (Binary,String,Byte,Short,Int,Long,Float,Double,BigInteger,BigDecimal,Date,UUID)= - /** - * @return {@code true} if the feature is of type $Type - *{%@ident%}/ - public boolean is$Type() { - return type == Type.{%@case:upper $Type%}; - } - %} - // - */ - // + // + // GENERATED CODE /** * @return {@code true} if the feature is of type Binary @@ -297,106 +280,80 @@ public boolean isUUID() { } // - // __END__ - - /*!jamal - // - {%@comment UUID and BigDecimal are handled separately, because they differ significantly%}\ - {%!@for [separator="\\s*\n\\s*" skipEmpty]($Type,$return,$rType) in `LOOP` - Binary|value|byte[] - String|new String(value, StandardCharsets.UTF_8)|String - Byte|value[0]|byte - Short|ByteBuffer.wrap(value).getShort()|short - Int|ByteBuffer.wrap(value).getInt()|int - Long|ByteBuffer.wrap(value).getLong()|long - Float|ByteBuffer.wrap(value).getFloat()|float - Double|ByteBuffer.wrap(value).getDouble()|double - BigInteger|new BigInteger(value)|BigInteger - Date|new Date(ByteBuffer.wrap(value).getLong())|Date - `LOOP`= - public $rType get$Type() { - if (type != Type.{%@case:upper $Type%}) { - throw new IllegalArgumentException("Feature is not {%@case:upper $Type%}"); - } - return $return; - } - %} - // - */ - // + // + public byte[] getBinary() { if (type != Type.BINARY) { throw new IllegalArgumentException("Feature is not BINARY"); } return value; } - + public String getString() { if (type != Type.STRING) { throw new IllegalArgumentException("Feature is not STRING"); } return new String(value, StandardCharsets.UTF_8); } - + public byte getByte() { if (type != Type.BYTE) { throw new IllegalArgumentException("Feature is not BYTE"); } return value[0]; } - + public short getShort() { if (type != Type.SHORT) { throw new IllegalArgumentException("Feature is not SHORT"); } return ByteBuffer.wrap(value).getShort(); } - + public int getInt() { if (type != Type.INT) { throw new IllegalArgumentException("Feature is not INT"); } return ByteBuffer.wrap(value).getInt(); } - + public long getLong() { if (type != Type.LONG) { throw new IllegalArgumentException("Feature is not LONG"); } return ByteBuffer.wrap(value).getLong(); } - + public float getFloat() { if (type != Type.FLOAT) { throw new IllegalArgumentException("Feature is not FLOAT"); } return ByteBuffer.wrap(value).getFloat(); } - + public double getDouble() { if (type != Type.DOUBLE) { throw new IllegalArgumentException("Feature is not DOUBLE"); } return ByteBuffer.wrap(value).getDouble(); } - + public BigInteger getBigInteger() { if (type != Type.BIGINTEGER) { throw new IllegalArgumentException("Feature is not BIGINTEGER"); } return new BigInteger(value); } - + public Date getDate() { if (type != Type.DATE) { throw new IllegalArgumentException("Feature is not DATE"); } return new Date(ByteBuffer.wrap(value).getLong()); } - + // - // __END__ public BigDecimal getBigDecimal() { if (type != Type.BIGDECIMAL) { throw new IllegalArgumentException("Feature is not BIGDECIMAL"); @@ -417,29 +374,8 @@ public UUID getUUID() { return new UUID(ms, ls); } - /*!jamal - private enum Type {{%@counter:define id=typeSerial%}\ - {%!@for [separator="\\s*\n\\s*" skipEmpty]($type,$length,$cast,$stringer,$unstringer,$sep) in `LOOP` - binary|VARIABLE_LENGTH|byte[]|ba -> Base64.getEncoder().encodeToString((byte[]) ba)|enc -> Base64.getDecoder().decode(enc)| - string|VARIABLE_LENGTH|||s -> s| - byte|||b -> String.format("0x%02X", (Byte) b)|NumericParser.Byte::parse| - short||||NumericParser.Short::parse| - int|Integer.BYTES|Integer||NumericParser.Int::parse| - long||||NumericParser.Long::parse| - float||||Float::parseFloat| - double||||Double::parseDouble| - bigInteger|VARIABLE_LENGTH|||BigInteger::new| - bigDecimal|VARIABLE_LENGTH|||BigDecimal::new| - date|Long.BYTES||Feature::dateFormat|Feature::dateParse| - UUID|2 * Long.BYTES|java.util.UUID||java.util.UUID::fromString|; - `LOOP`= - {%@case:upper $type%}({%typeSerial%}, {%#if/$length/$length/{%@case:cap $type%}.BYTES%}, - Feature::get{%@case:cap $type%}, - (name, value) -> Create.{%@replace /$type/UUID/uuid%}Feature(name, ({%#if/$cast/$cast/{%@case:cap $type%}%}) value), - {%#if/$stringer/$stringer/Object::toString%}, $unstringer){%@if/$sep/$sep/,%} -%} - */ - private enum Type { + // +private enum Type { BINARY(1, VARIABLE_LENGTH, Feature::getBinary, (name, value) -> Create.binaryFeature(name, (byte[]) value), @@ -500,7 +436,8 @@ private enum Type { (name, value) -> Create.uuidFeature(name, (java.util.UUID) value), Object::toString, java.util.UUID::fromString); - //__END__ + + // /** * Every type has a serialized value that is used in the serialized format. This value is fixed and does not * depend on the ordinal value of the enum. This is to keep the serialized format backward compatible if ever @@ -554,38 +491,11 @@ public static class Create { private Create() { } - /*!jamal - // - {%!@for [separator="\\s*\n\\s*" skipEmpty] ($type,$vType,$value) in `LOOP` -Binary|byte[]|value -String|String|value.getBytes(StandardCharsets.UTF_8) -Byte|Byte|new byte[]{value} -Short|Short|ByteBuffer.allocate(Short.BYTES).putShort(value).array() -Int|Integer|ByteBuffer.allocate(Integer.BYTES).putInt(value).array() -Long|Long|ByteBuffer.allocate(Long.BYTES).putLong(value).array() -Float|Float|ByteBuffer.allocate(Float.BYTES).putFloat(value).array() -Double|Double|ByteBuffer.allocate(Double.BYTES).putDouble(value).array() -BigInteger|BigInteger|value.toByteArray() -uuid|java.util.UUID|ByteBuffer.allocate(2 * Long.BYTES).putLong(value.getLeastSignificantBits()).putLong(value.getMostSignificantBits()).array() -Date|Date|ByteBuffer.allocate(Long.BYTES).putLong(value.getTime()).array() - `LOOP`= - /** - * Create a new {%@case:decap $type%} feature. - * @param name the name of the new feature - * @param value the value for the new feature. {@code null} value will throw an exception - * @return the newly created feature object - *{%@comment%}/ - public static Feature {%@case:decap $type%}Feature(String name, $vType value) { - Objects.requireNonNull(value); - return new Feature(name, Type.{%@case:upper $type%}, $value); - } -%} - // - */ - // + // /** * Create a new binary feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -597,6 +507,7 @@ public static Feature binaryFeature(String name, byte[] value) { /** * Create a new string feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -608,6 +519,7 @@ public static Feature stringFeature(String name, String value) { /** * Create a new byte feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -619,6 +531,7 @@ public static Feature byteFeature(String name, Byte value) { /** * Create a new short feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -630,6 +543,7 @@ public static Feature shortFeature(String name, Short value) { /** * Create a new int feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -641,6 +555,7 @@ public static Feature intFeature(String name, Integer value) { /** * Create a new long feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -652,6 +567,7 @@ public static Feature longFeature(String name, Long value) { /** * Create a new float feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -663,6 +579,7 @@ public static Feature floatFeature(String name, Float value) { /** * Create a new double feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -674,6 +591,7 @@ public static Feature doubleFeature(String name, Double value) { /** * Create a new bigInteger feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -685,6 +603,7 @@ public static Feature bigIntegerFeature(String name, BigInteger value) { /** * Create a new uuid feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -696,6 +615,7 @@ public static Feature uuidFeature(String name, java.util.UUID value) { /** * Create a new date feature. + * * @param name the name of the new feature * @param value the value for the new feature. {@code null} value will throw an exception * @return the newly created feature object @@ -705,8 +625,8 @@ public static Feature dateFeature(String name, Date value) { return new Feature(name, Type.DATE, ByteBuffer.allocate(Long.BYTES).putLong(value.getTime()).array()); } + // - //__END__ /** * Create a new BigDecimal feature. @@ -719,9 +639,9 @@ public static Feature bigDecimalFeature(String name, BigDecimal value) { Objects.requireNonNull(value); byte[] b = value.unscaledValue().toByteArray(); return new Feature(name, Type.BIGDECIMAL, ByteBuffer.allocate(Integer.BYTES + b.length) - .put(b) - .putInt(value.scale()) - .array()); + .put(b) + .putInt(value.scale()) + .array()); } /** @@ -787,7 +707,7 @@ public static Feature from(byte[] serialized) { public static void throwBinaryWayTooShort(int len) { throw new IllegalArgumentException("Cannot load feature from a byte array that has " - + len + " bytes which is < " + (2 * Integer.BYTES)); + + len + " bytes which is < " + (2 * Integer.BYTES)); } public static void throwBinaryTooShort(int len) { @@ -796,7 +716,7 @@ public static void throwBinaryTooShort(int len) { public static void throwBinaryTooLong(int len, int extra) { throw new IllegalArgumentException("Cannot load feature from a byte array that has " - + len + " bytes which is " + extra + " bytes too long"); + + len + " bytes which is " + extra + " bytes too long"); } public static void throwBinaryTooLong(String s) { diff --git a/src/main/java/javax0/license3j/crypto/LicenseKeyPair.java b/src/main/java/javax0/license3j/crypto/LicenseKeyPair.java index fb32d94..3c886e9 100644 --- a/src/main/java/javax0/license3j/crypto/LicenseKeyPair.java +++ b/src/main/java/javax0/license3j/crypto/LicenseKeyPair.java @@ -2,13 +2,7 @@ import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; -import java.security.Key; -import java.security.KeyFactory; -import java.security.KeyPair; -import java.security.KeyPairGenerator; -import java.security.NoSuchAlgorithmException; -import java.security.PrivateKey; -import java.security.PublicKey; +import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; @@ -53,31 +47,11 @@ public KeyPair getPair() { return pair; } - /*!jamal - // - {%!@for ($Type) in (Public,Private)= - /** - * Get the byte representation of the {%@case:lower $Type%} key as it is returned - * by the underlying security library. This is NOT the byte array - * that contains the algorithm at the start. This is the key in raw - * format. - * - * @return the key as bytes - *{%@comment%}/ - public byte[] get$Type() { - keyNotNull(pair.get$Type()); - final var key = pair.get$Type(); - return getKeyBytes(key); - } - %} - // - */ - // - - /** + // + /**GENERATED CODE * Get the byte representation of the public key as it is returned - * by the underlying security library. This is NOT the byte array - * that contains the algorithm at the start. This is the key in raw + * by the underlying security library. It is NOT the byte array + * that contains the algorithm at the start. It is the key in raw * format. * * @return the key as bytes @@ -87,11 +61,10 @@ public byte[] getPublic() { final var key = pair.getPublic(); return getKeyBytes(key); } - - /** + /**GENERATED CODE * Get the byte representation of the private key as it is returned - * by the underlying security library. This is NOT the byte array - * that contains the algorithm at the start. This is the key in raw + * by the underlying security library. It is NOT the byte array + * that contains the algorithm at the start. It is the key in raw * format. * * @return the key as bytes @@ -102,8 +75,9 @@ public byte[] getPrivate() { return getKeyBytes(key); } - // - //__END__ + +// + private byte[] getKeyBytes(final Key key) { final var algorithm = cipherTransformation.getBytes(StandardCharsets.UTF_8); @@ -148,7 +122,7 @@ public static LicenseKeyPair from(final KeyPair keyPair, final String cipher) { private static String algorithmPrefix(final String cipher) { if (cipher.contains("/")) { - return cipher .substring(0, cipher.indexOf("/")); + return cipher.substring(0, cipher.indexOf("/")); } else { return cipher; } @@ -157,7 +131,7 @@ private static String algorithmPrefix(final String cipher) { /** * Create a new key pair using the algorithm and the size for the key. The cipher transformation may optionally * contain the mode and the padding as {@code algorithm/mode/padding}. Note that the mode and the padding is - * not needed for the key generation. Nevertheless these are also stored in the generated {@link LicenseKeyPair} + * not needed for the key generation. Nevertheless, these are also stored in the generated {@link LicenseKeyPair} * and will be used to sign/verify the license. *

* It is recommended to use the full cipher. Using only the algorithm will let the encryption provider to @@ -181,16 +155,17 @@ public static LicenseKeyPair from(final String cipher, final int size) throws No /** * Create a new {@link LicenseKeyPair} that contains only one of the keys, either the private or the public. * The key is provided in binary format as a byte array. + * * @param encoded the key encoded as byte array. This is the format that contains the algorithm at the start * of the byte array as zero byte terminated string. - * @param type either 1 for public key specification or 2 for private key specification. These commands are - * defined in the JDK in the {@link Modifier} class as {@link Modifier#PUBLIC} and - * {@link Modifier#PRIVATE}. + * @param type either 1 for public key specification or 2 for private key specification. These commands are + * defined in the JDK in the {@link Modifier} class as {@link Modifier#PUBLIC} and + * {@link Modifier#PRIVATE}. * @return the newly created {@link LicenseKeyPair} * @throws NoSuchAlgorithmException if the algorithm in the encoded key is not implemented by the actual - * encryption provider - * @throws InvalidKeySpecException if the bytes of the key are garbage and cannot be decoded by the actual - * encryption provider. + * encryption provider + * @throws InvalidKeySpecException if the bytes of the key are garbage and cannot be decoded by the actual + * encryption provider. */ public static LicenseKeyPair from(final byte[] encoded, final int type) throws NoSuchAlgorithmException, InvalidKeySpecException { final String cipher = getCipher(encoded); @@ -202,18 +177,19 @@ public static LicenseKeyPair from(final byte[] encoded, final int type) throws N /** * Create a new {@link LicenseKeyPair} from the public and the private key pairs. - * - * The method does not check that the encoded cipher specification at the start of the keys are the same or not. + *

+ * The method does not check that the encoded cipher specification at the start of the keys is the same or not. * They are supposed to be the same. There is also no check that the private and the public key are a pair. + * * @param privateEncoded the private key encoded including the cipher specification with a zero byte * terminated at the start of the byte array - * @param publicEncoded the public key encoded including the cipher specification with a zero byte - * terminated at the start of the byte array + * @param publicEncoded the public key encoded including the cipher specification with a zero byte + * terminated at the start of the byte array * @return the newly created {@link LicenseKeyPair} * @throws NoSuchAlgorithmException if the algorithm in the encoded key is not implemented by the actual - * encryption provider - * @throws InvalidKeySpecException if the bytes of the key are garbage and cannot be decoded by the actual - * encryption provider. + * encryption provider + * @throws InvalidKeySpecException if the bytes of the key are garbage and cannot be decoded by the actual + * encryption provider. */ public static LicenseKeyPair from(final byte[] privateEncoded, final byte[] publicEncoded) throws NoSuchAlgorithmException, InvalidKeySpecException { final String cipher = getCipher(publicEncoded); diff --git a/src/main/java/javax0/license3j/hardware/Network.java b/src/main/java/javax0/license3j/hardware/Network.java index b87feb8..af7d1e2 100644 --- a/src/main/java/javax0/license3j/hardware/Network.java +++ b/src/main/java/javax0/license3j/hardware/Network.java @@ -1,5 +1,6 @@ package javax0.license3j.hardware; +import java.net.NetworkInterface; import java.net.SocketException; import java.util.Collections; import java.util.Enumeration; @@ -16,50 +17,37 @@ private NetworkInterface(java.net.NetworkInterface ni) { this.ni = ni; } - /*!jamal - {%!@for [separator="\\s*\n\\s*" skipEmpty]($rettype,$name,$exceptions) in - `LOOP` - String|getName| - byte[]|getHardwareAddress|SocketException - String|getDisplayName| - boolean|isLoopback|SocketException - boolean|isVirtual|SocketException - boolean|isPointToPoint|SocketException - `LOOP`= - public $rettype $name(){%#if/$exceptions/ throws $exceptions/%}{ - return ni.$name(); - } - %} - */ - - public String getName(){ + // + + public String getName() { return ni.getName(); - } - - public byte[] getHardwareAddress() throws SocketException{ + } + + public byte[] getHardwareAddress() throws SocketException { return ni.getHardwareAddress(); - } - - public String getDisplayName(){ + } + + public String getDisplayName() { return ni.getDisplayName(); - } - - public boolean isLoopback() throws SocketException{ + } + + public boolean isLoopback() throws SocketException { return ni.isLoopback(); - } - - public boolean isVirtual() throws SocketException{ + } + + public boolean isVirtual() throws SocketException { return ni.isVirtual(); - } - - public boolean isPointToPoint() throws SocketException{ + } + + public boolean isPointToPoint() throws SocketException { return ni.isPointToPoint(); - } + } + - //__END__ + // public static Enumeration getNetworkInterfaces() - throws SocketException { + throws SocketException { return Collections.enumeration(Collections.list(java.net.NetworkInterface.getNetworkInterfaces()).stream().map(ni -> new NetworkInterface(ni)).collect(Collectors.toList())); } } @@ -80,8 +68,8 @@ private Data(final NetworkInterface networkInterface) { static Stream gatherUsing(Network.Interface.Selector selector) throws SocketException { return Collections.list(NetworkInterface.getNetworkInterfaces()).stream() - .filter(selector::usable) - .map(Network.Interface.Data::new); + .filter(selector::usable) + .map(Network.Interface.Data::new); } } @@ -128,9 +116,9 @@ private boolean matchesRegexLists(final NetworkInterface netIf) { final String name = netIf.getDisplayName(); return !matchesAny(name, deniedInterfaceNames) - && - (allowedInterfaceNames.isEmpty() || - matchesAny(name, allowedInterfaceNames)); + && + (allowedInterfaceNames.isEmpty() || + matchesAny(name, allowedInterfaceNames)); } /** @@ -165,7 +153,7 @@ public void interfaceDenied(String regex) { */ boolean usable(final NetworkInterface netIf) { return !isSpecial(netIf) - && matchesRegexLists(netIf); + && matchesRegexLists(netIf); } /**