diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..261eeb9
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..59f02e8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,19 @@
+
+# uid.tools
+
+pom.xml:
+
+1. in repositories section:
+
+
+ ua.net.uid.releases
+ https://git.uid.net.ua/maven/releases/
+
+
+2. in dependencies section:
+
+
+ ua.net.uid
+ uid.tools
+ 1.0.0
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..5a98a95
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,119 @@
+
+
+ 4.0.0
+ ua.net.uid
+ uid.security
+ 1.0.1-SNAPSHOT
+ jar
+
+
+
+ ASL
+ http://www.apache.org/licenses/LICENSE-2.0.txt
+ repo
+
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ 5.5.1
+ test
+
+
+ com.h2database
+ h2
+ [1.4.193, 1.5.0)
+ test
+
+
+ ua.net.uid
+ uid.tools
+ 1.0.0
+ test
+
+
+
+
+
+
+ .
+
+ LICENSE
+ NOTICE.txt
+ RELEASE.txt
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.0
+
+ 1.8
+ 1.8
+ true
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+ maven-failsafe-plugin
+ 2.22.2
+
+
+ org.apache.maven.plugins
+ maven-release-plugin
+ 2.5.3
+
+ true
+ @{project.version}
+ release
+
+
+
+
+
+
+ UTF-8
+ 1.8
+ 1.8
+ uid-releases
+
+
+
+
+ ua.net.uid-releases
+ https://git.uid.net.ua/maven/releases/
+
+
+ ua.net.uid-snapshots
+ https://git.uid.net.ua/maven/snapshots/
+
+
+
+
+ scm:git:https://git.uid.net.ua/git/uid/uid.security.git
+ scm:git:https://git.uid.net.ua/git/uid/uid.security.git
+ https://git.uid.net.ua/git/uid/uid.security.git
+
+
+
+
+ uid-releases
+ Release repository
+ https://git.uid.net.ua/maven/releases/
+
+
+ uid-snapshots
+ Snapshots repository
+ https://git.uid.net.ua/maven/snapshots/
+
+
+
diff --git a/src/main/java/ua/net/uid/utils/security/keccak/Keccak.java b/src/main/java/ua/net/uid/utils/security/keccak/Keccak.java
new file mode 100644
index 0000000..5ca68d7
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/security/keccak/Keccak.java
@@ -0,0 +1,245 @@
+package ua.net.uid.utils.security.keccak;
+
+public class Keccak {
+ private static final long[] RC = {
+ 0x0000000000000001L, 0x0000000000008082L, 0x800000000000808AL, 0x8000000080008000L,
+ 0x000000000000808BL, 0x0000000080000001L, 0x8000000080008081L, 0x8000000000008009L,
+ 0x000000000000008AL, 0x0000000000000088L, 0x0000000080008009L, 0x000000008000000AL,
+ 0x000000008000808BL, 0x800000000000008BL, 0x8000000000008089L, 0x8000000000008003L,
+ 0x8000000000008002L, 0x8000000000000080L, 0x000000000000800AL, 0x800000008000000AL,
+ 0x8000000080008081L, 0x8000000000008080L, 0x0000000080000001L, 0x8000000080008008L
+ };
+ private static final int[] PILN = {
+ 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
+ 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
+ };
+ private static final int[] ROTC = {
+ 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14,
+ 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44
+ };
+ ////////////////////////////////////////////////////////////////////////////
+ private final long[] matrix = new long[25];
+ private final int size;
+ private final int output;
+ private final byte suffix;
+ private int count;
+
+ Keccak(int size, int output, byte suffix) {
+ this.size = size;
+ this.output = output;
+ this.suffix = suffix;
+ }
+
+ private static long rot(long val, int offset) {
+ return (val << offset) ^ (val >>> (64 - offset));
+ }
+
+ public Keccak update(byte input) {
+ matrix[count >>> 3] ^= ((long) input) << ((count & 7) << 3);
+ if (++count == size)
+ permute();
+ return this;
+ }
+
+ public Keccak update(byte[] input) {
+ update(input, 0, input.length);
+ return this;
+ }
+
+ public Keccak update(byte[] input, int offset, int length) {
+ //TODO update(byte[] input, int offset, int length)
+ for (; offset < length; ++offset)
+ update(input[offset]);
+ return this;
+ }
+
+ public byte[] digest() {
+ padding();
+ byte[] out = new byte[output];
+ int offset = 0;
+ do {
+ permute();
+ int len = Math.min(output - offset, size);
+ for (int i = 0; i < len; ++i, ++offset) {
+ out[offset] = (byte) ((matrix[i >>> 3] >>> ((i & 7) << 3)) & 255);
+ }
+ } while (offset < output);
+ reset();
+ return out;
+ }
+
+ public byte[] digest(byte[] input) {
+ return digest(input, 0, input.length);
+ }
+
+ public byte[] digest(byte[] input, int offset, int length) {
+ update(input, offset, length);
+ return digest();
+ }
+
+ public void reset() {
+ for (int i = 0; i < 25; ++i)
+ matrix[i] = 0;
+ count = 0;
+ }
+
+ protected void padding() {
+ matrix[count >>> 3] ^= ((long) suffix) << ((count & 7) << 3);
+ int last = size - 1;
+ if ((suffix & 0x80) != 0 && count == last)
+ permute();
+ matrix[last >>> 3] ^= 0x80L << ((last & 7) << 3);
+ }
+
+ protected final void permute() {
+ long[] tmp = new long[5];
+ long d;
+ for (int r = 0; r < 24; ++r) {
+ // θ step
+ for (int i = 0; i < 5; ++i)
+ tmp[i] = matrix[i] ^ matrix[i + 5] ^ matrix[i + 10] ^ matrix[i + 15] ^ matrix[i + 20];
+ d = tmp[4] ^ rot(tmp[1], 1);
+ for (int j = 0; j < 25; j += 5) matrix[j] ^= d;
+ d = tmp[0] ^ rot(tmp[2], 1);
+ for (int j = 1; j < 25; j += 5) matrix[j] ^= d;
+ d = tmp[1] ^ rot(tmp[3], 1);
+ for (int j = 2; j < 25; j += 5) matrix[j] ^= d;
+ d = tmp[2] ^ rot(tmp[4], 1);
+ for (int j = 3; j < 25; j += 5) matrix[j] ^= d;
+ d = tmp[3] ^ rot(tmp[0], 1);
+ for (int j = 4; j < 25; j += 5) matrix[j] ^= d;
+ // ρ and π steps
+ d = matrix[1];
+ for (int i = 0; i < 24; ++i) {
+ int j = PILN[i];
+ long t = matrix[j];
+ matrix[j] = rot(d, ROTC[i]);
+ d = t;
+ }
+ // χ step
+ for (int j = 0; j < 25; j += 5) {
+ //for (int i = 0; i < 5; i++) tmp[i] = matrix[j + i];
+ System.arraycopy(matrix, j, tmp, 0, 5);
+ matrix[j] = tmp[0] ^ ((~tmp[1]) & tmp[2]);
+ matrix[j + 1] = tmp[1] ^ ((~tmp[2]) & tmp[3]);
+ matrix[j + 2] = tmp[2] ^ ((~tmp[3]) & tmp[4]);
+ matrix[j + 3] = tmp[3] ^ ((~tmp[4]) & tmp[0]);
+ matrix[j + 4] = tmp[4] ^ ((~tmp[0]) & tmp[1]);
+ }
+ // ι step
+ matrix[0] ^= RC[r];
+ }
+ count = 0;
+ }
+
+ //super("Keccak.CryptoProvider", 1.0, "Keccak 64/128/160/224/256/384/512/640/768 bit digest");
+ ////////////////////////////////////////////////////////////////////////////
+ public static class Base64 extends Base {
+ public Base64() {
+ super(8);
+ }
+ }
+
+ public static class Base128 extends Base {
+ public Base128() {
+ super(16);
+ }
+ }
+
+ public static class Base160 extends Base {
+ public Base160() {
+ super(20);
+ }
+ }
+
+ public static class Base224 extends Base {
+ public Base224() {
+ super(28);
+ }
+ }
+
+ public static class Base256 extends Base {
+ public Base256() {
+ super(32);
+ }
+ }
+
+ public static class Base384 extends Base {
+ public Base384() {
+ super(48);
+ }
+ }
+
+ public static class Base512 extends Base {
+ public Base512() {
+ super(64);
+ }
+ }
+
+ public static class Base640 extends Base {
+ public Base640() {
+ super(80);
+ }
+ }
+
+ public static class Base768 extends Base {
+ public Base768() {
+ super(96);
+ }
+ }
+
+ public static class SHA3_224 extends SHA3 {
+ public SHA3_224() {
+ super(224 / 8);
+ }
+ }
+
+ public static class SHA3_256 extends SHA3 {
+ public SHA3_256() {
+ super(256 / 8);
+ }
+ }
+
+ public static class SHA3_384 extends SHA3 {
+ public SHA3_384() {
+ super(384 / 8);
+ }
+ }
+
+ public static class SHA3_512 extends SHA3 {
+ public SHA3_512() {
+ super(512 / 8);
+ }
+ }
+
+ public static class SHAKE128 extends SHAKE {
+ public SHAKE128(int output) {
+ super(128 / 8, output);
+ }
+ }
+
+ public static class SHAKE256 extends SHAKE {
+ public SHAKE256(int output) {
+ super(256 / 8, output);
+ }
+ }
+
+ ////////////////////////////////////////////////////////////////////////////
+ static class Base extends Keccak {
+ public Base(int output) {
+ super(200 - output * 2, output, (byte) 1);
+ }
+ }
+
+ static class SHA3 extends Keccak {
+ public SHA3(int output) {
+ super(200 - output * 2, output, (byte) 6);
+ }
+ }
+
+ static class SHAKE extends Keccak {
+ public SHAKE(int size, int output) {
+ super(200 - size * 2, output, (byte) 31);
+ }
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/security/keccak/KeccakDigest.java b/src/main/java/ua/net/uid/utils/security/keccak/KeccakDigest.java
new file mode 100644
index 0000000..dc8a8eb
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/security/keccak/KeccakDigest.java
@@ -0,0 +1,123 @@
+package ua.net.uid.utils.security.keccak;
+
+import java.security.MessageDigest;
+
+public abstract class KeccakDigest extends MessageDigest {
+ private final Keccak keccak;
+
+ KeccakDigest(String name, Keccak instance) {
+ super(name);
+ keccak = instance;
+ //engineReset();
+ }
+
+ @Override
+ protected void engineUpdate(byte input) {
+ keccak.update(input);
+ }
+
+ @Override
+ protected void engineUpdate(byte[] input, int offset, int length) {
+ keccak.update(input, offset, length);
+ }
+
+ @Override
+ protected byte[] engineDigest() {
+ return keccak.digest();
+ }
+
+ @Override
+ protected void engineReset() {
+ keccak.reset();
+ }
+
+ public static class Keccak64 extends KeccakDigest {
+ public Keccak64() {
+ super("Keccak-64", new Keccak.Base64());
+ }
+ }
+
+ public static class Keccak128 extends KeccakDigest {
+ public Keccak128() {
+ super("Keccak-128", new Keccak.Base128());
+ }
+ }
+
+ public static class Keccak160 extends KeccakDigest {
+ public Keccak160() {
+ super("Keccak-160", new Keccak.Base160());
+ }
+ }
+
+ public static class Keccak224 extends KeccakDigest {
+ public Keccak224() {
+ super("Keccak-224", new Keccak.Base224());
+ }
+ }
+
+ public static class Keccak256 extends KeccakDigest {
+ public Keccak256() {
+ super("Keccak-256", new Keccak.Base256());
+ }
+ }
+
+ public static class Keccak384 extends KeccakDigest {
+ public Keccak384() {
+ super("Keccak-384", new Keccak.Base384());
+ }
+ }
+
+ public static class Keccak512 extends KeccakDigest {
+ public Keccak512() {
+ super("Keccak-512", new Keccak.Base512());
+ }
+ }
+
+ public static class Keccak640 extends KeccakDigest {
+ public Keccak640() {
+ super("Keccak-640", new Keccak.Base640());
+ }
+ }
+
+ public static class Keccak768 extends KeccakDigest {
+ public Keccak768() {
+ super("Keccak-768", new Keccak.Base768());
+ }
+ }
+
+ public static class SHA3_224 extends KeccakDigest {
+ public SHA3_224() {
+ super("SHA3-224", new Keccak.SHA3_224());
+ }
+ }
+
+ public static class SHA3_256 extends KeccakDigest {
+ public SHA3_256() {
+ super("SHA3-256", new Keccak.SHA3_256());
+ }
+ }
+
+ public static class SHA3_384 extends KeccakDigest {
+ public SHA3_384() {
+ super("SHA3-384", new Keccak.SHA3_384());
+ }
+ }
+
+ public static class SHA3_512 extends KeccakDigest {
+ public SHA3_512() {
+ super("SHA3-512", new Keccak.SHA3_512());
+ }
+ }
+
+ public static class SHAKE128 extends KeccakDigest {
+ public SHAKE128(int output) {
+ super("SHAKE-128", new Keccak.SHAKE128(output));
+ }
+ }
+
+ public static class SHAKE256 extends KeccakDigest {
+ public SHAKE256(int output) {
+ super("SHAKE-256", new Keccak.SHAKE256(output));
+ }
+ }
+}
diff --git a/src/main/java/ua/net/uid/utils/security/keccak/KeccakProvider.java b/src/main/java/ua/net/uid/utils/security/keccak/KeccakProvider.java
new file mode 100644
index 0000000..46669bc
--- /dev/null
+++ b/src/main/java/ua/net/uid/utils/security/keccak/KeccakProvider.java
@@ -0,0 +1,31 @@
+package ua.net.uid.utils.security.keccak;
+
+import java.security.Provider;
+import java.security.Security;
+
+// https://github.com/aelstad/keccakj
+
+public class KeccakProvider extends Provider {
+
+ KeccakProvider() {
+ super("ua.net.uid.utils.security.keccak", 1.0, "Keccak-*/SHA3-* digest");
+ put("MessageDigest.Keccak-64", KeccakDigest.Keccak64.class.getName()); // non-standard
+ put("MessageDigest.Keccak-128", KeccakDigest.Keccak128.class.getName()); // non-standard
+ put("MessageDigest.Keccak-160", KeccakDigest.Keccak160.class.getName()); // non-standard
+ put("MessageDigest.Keccak-224", KeccakDigest.Keccak224.class.getName());
+ put("MessageDigest.Keccak-256", KeccakDigest.Keccak256.class.getName());
+ put("MessageDigest.Keccak-384", KeccakDigest.Keccak384.class.getName());
+ put("MessageDigest.Keccak-512", KeccakDigest.Keccak512.class.getName());
+ put("MessageDigest.Keccak-640", KeccakDigest.Keccak640.class.getName()); // non-standard
+ put("MessageDigest.Keccak-768", KeccakDigest.Keccak768.class.getName()); // non-standard
+
+ put("MessageDigest.SHA3-224", KeccakDigest.SHA3_224.class.getName());
+ put("MessageDigest.SHA3-256", KeccakDigest.SHA3_256.class.getName());
+ put("MessageDigest.SHA3-384", KeccakDigest.SHA3_384.class.getName());
+ put("MessageDigest.SHA3-512", KeccakDigest.SHA3_512.class.getName());
+ }
+
+ public static void register() {
+ Security.addProvider(new KeccakProvider());
+ }
+}
diff --git a/src/test/java/ua/net/uid/utils/security/keccak/KeccakTest.java b/src/test/java/ua/net/uid/utils/security/keccak/KeccakTest.java
new file mode 100644
index 0000000..d071e3f
--- /dev/null
+++ b/src/test/java/ua/net/uid/utils/security/keccak/KeccakTest.java
@@ -0,0 +1,201 @@
+package ua.net.uid.utils.security.keccak;
+
+import org.junit.jupiter.api.Test;
+import ua.net.uid.utils.helpers.StringHelper;
+
+import java.io.UnsupportedEncodingException;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+
+// https://ru.wikipedia.org/wiki/SHA-3
+// http://csrc.nist.gov/groups/ST/toolkit/examples.html#aHashing
+// https://asecuritysite.com/encryption/s3
+
+//TODO - method test
+class KeccakTest {
+ private static final byte[] SINGLE_SRC = bytesFromString("q");
+ private static final byte[] SHORT_SRC = bytesFromString("The quick brown fox jumps over the lazy dog");
+ private static final byte[] LONG_SRC = bytesFromString("Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione voluptatem sequi nesciunt, neque porro quisquam est, qui dolorem ipsum, quia dolor sit, amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt, ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.");
+ private static final char[] SHA3_224_EMPTY = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7".toCharArray();
+ private static final char[] SHA3_224_SINGLE = "774057ce10e1b255cfa747982782e969231ef434a057622021ff5b9c".toCharArray();
+ private static final char[] SHA3_224_SHORT = "d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795".toCharArray();
+ private static final char[] SHA3_224_LONG = "4cacfb51ec5dbefe4b00eb480de1677b7d2ffbbabed3648fcbd1d90c".toCharArray();
+ private static final char[] SHA3_256_EMPTY = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a".toCharArray();
+ private static final char[] SHA3_256_SINGLE = "8a5e1d339fafc39350fd8cf1d7ca7982091c27f6b77f75bd4ddab3df425b4f8c".toCharArray();
+ private static final char[] SHA3_256_SHORT = "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04".toCharArray();
+ private static final char[] SHA3_256_LONG = "2c930fe119b1b8b2887630e4d5c4332d8c3075c57ebd26ebcea5cb064e77426d".toCharArray();
+ private static final char[] SHA3_384_EMPTY = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004".toCharArray();
+ private static final char[] SHA3_384_SINGLE = "a73cf129eac67f6b9e2f3818b3b845572914c3c6821fafdc71d834f7852ba1c1d894c1a1d71669b9090d1a08418d34d9".toCharArray();
+ private static final char[] SHA3_384_SHORT = "7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41".toCharArray();
+ private static final char[] SHA3_384_LONG = "d6f446bb12cf2b98ec22ab8c7ef9a4a95effc4860b1dca5996273d8829e3cb558d4e95b39d09d6ce3f7037d45ca63c8a".toCharArray();
+ private static final char[] SHA3_512_EMPTY = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26".toCharArray();
+ private static final char[] SHA3_512_SINGLE = "f435ba3ef2bf43e694c8940fa315641c67f152c2ee2021f121af5e03f9860607f74e61e1451f9489c2ff59f87dc0e1c501566e2324355de32770ec52cc3bce47".toCharArray();
+ private static final char[] SHA3_512_SHORT = "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450".toCharArray();
+ private static final char[] SHA3_512_LONG = "350cb956d6f052728478ce111835746e39a2f1a7f5c907842f348a875f76baec54e2bb1c77c8efa3512292adad7be44bbd5b11cca04460a868dc3a107d77a6ac".toCharArray();
+ private static final char[] SHAKE128_EMPTY = "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26".toCharArray();
+ private static final char[] SHAKE128_SINGLE = "87b8d13b05e63515cb6ee34685ece7c3be88a65f5b68949a523b0cd68855e716".toCharArray();
+ private static final char[] SHAKE128_SHORT = "f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e".toCharArray();
+ private static final char[] SHAKE128_LONG = "be233a11ef0a126b0672f9244bdd5a03e6004d3d92b11dfd9ee1be780d8c8c67".toCharArray();
+ private static final char[] SHAKE256_EMPTY = "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be".toCharArray();
+ private static final char[] SHAKE256_SINGLE = "2d8e1bec7cd9bdaddff81fac8bf5773f7413082c1bb11d415b23bf376f872ba21265ddea3bee44089e3fd5b584ae71500d71bc1825bcacfa7f7b5b42d254f5d0".toCharArray();
+ private static final char[] SHAKE256_SHORT = "2f671343d9b2e1604dc9dcf0753e5fe15c7c64a0d283cbbf722d411a0e36f6ca1d01d1369a23539cd80f7c054b6e5daf9c962cad5b8ed5bd11998b40d5734442".toCharArray();
+ private static final char[] SHAKE256_LONG = "dbb99d82f2e750a023a7226ffe4dfcd77814251722a426cd09c4296174981913d07abebe1b393ecba401f7ad46ea805def00bb72e10f53ee43b5e97912fea9be".toCharArray();
+ private static final char[] KECCAK_224_EMPTY = "f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd".toCharArray();
+ private static final char[] KECCAK_224_SINGLE = "ba3d4f9f6f0d79ab054481e9002180d4a91ced1284b398f96ca69533".toCharArray();
+ private static final char[] KECCAK_224_SHORT = "310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe".toCharArray();
+ private static final char[] KECCAK_224_LONG = "797afa62e49626395acf880b845a7167bc71ce77dc30b95fa6af19e2".toCharArray();
+ private static final char[] KECCAK_256_EMPTY = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470".toCharArray();
+ private static final char[] KECCAK_256_SINGLE = "3ff269d37634c240a40e1b0de0d61faffb6bbb3c251727e2ef176a979d8b95ff".toCharArray();
+ private static final char[] KECCAK_256_SHORT = "4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15".toCharArray();
+ private static final char[] KECCAK_256_LONG = "4221cb6d55808ad673f358359c3ec9e884f21a8b5b34a3964b7e5b471ea3f924".toCharArray();
+ private static final char[] KECCAK_384_EMPTY = "2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff".toCharArray();
+ private static final char[] KECCAK_384_SINGLE = "dbd9922428df7bab1ade9829cdf1eb766896aca88a5a7fe2f8b89e5164f858dab4522a2ae05df75a7b6c52f13013a215".toCharArray();
+ private static final char[] KECCAK_384_SHORT = "283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3".toCharArray();
+ private static final char[] KECCAK_384_LONG = "9b588b0e9fddb86d8521c464abdc8befb0dc3528f204b63539feb0f211c9ad82f095c0ad86e47267d6a52b30afbccfba".toCharArray();
+ private static final char[] KECCAK_512_EMPTY = "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e".toCharArray();
+ private static final char[] KECCAK_512_SINGLE = "d0304a5fe2c8844b1d68970acfb15ff145c89fb0039a0302931b6ecd00833c55b8acebb6d86d48885d7836767aa0e12810149f7bf41ae1397eea48720c7242ee".toCharArray();
+ private static final char[] KECCAK_512_SHORT = "d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609".toCharArray();
+ private static final char[] KECCAK_512_LONG = "ca84a077b175b94c4fac8162573b5c8ee5f1bf34f543a80abe4838e628a83ed4e6d2681baa81ee138fc93441c2c38d893d6f8231f3746c30faea3a9be909bc8a".toCharArray();
+
+ KeccakTest() {
+ /*Provider[] providers = Security.getProviders();
+ for (Provider provider : providers) {
+ showHashAlgorithms(provider, MessageDigest.class);
+ }*/
+ }
+
+ private static byte[] bytesFromString(String text) {
+ try {
+ return text.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException ex) {
+ return null;
+ }
+ }
+
+ @Test
+ void testSHA3_224() {
+ Keccak instance = new Keccak.SHA3_224();
+ assertArrayEquals(SHA3_224_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHA3_224_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHA3_224_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHA3_224_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testSHA3_256() {
+ Keccak instance = new Keccak.SHA3_256();
+ assertArrayEquals(SHA3_256_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHA3_256_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHA3_256_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHA3_256_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testSHA3_384() {
+ Keccak instance = new Keccak.SHA3_384();
+ assertArrayEquals(SHA3_384_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHA3_384_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHA3_384_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHA3_384_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testSHA3_512() {
+ Keccak instance = new Keccak.SHA3_512();
+ assertArrayEquals(SHA3_512_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHA3_512_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHA3_512_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHA3_512_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testSHAKE128() {
+ Keccak instance = new Keccak.SHAKE128(32);
+ assertArrayEquals(SHAKE128_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHAKE128_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHAKE128_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHAKE128_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testSHAKE256() {
+ Keccak instance = new Keccak.SHAKE256(64);
+ assertArrayEquals(SHAKE256_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(SHAKE256_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(SHAKE256_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(SHAKE256_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testKECCAK_224() {
+ Keccak instance = new Keccak.Base224();
+ assertArrayEquals(KECCAK_224_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(KECCAK_224_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(KECCAK_224_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(KECCAK_224_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testKECCAK_256() {
+ Keccak instance = new Keccak.Base256();
+ assertArrayEquals(KECCAK_256_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(KECCAK_256_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(KECCAK_256_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(KECCAK_256_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testKECCAK_384() {
+ Keccak instance = new Keccak.Base384();
+ assertArrayEquals(KECCAK_384_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(KECCAK_384_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(KECCAK_384_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(KECCAK_384_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+ @Test
+ void testKECCAK_512() {
+ Keccak instance = new Keccak.Base512();
+ assertArrayEquals(KECCAK_512_EMPTY, StringHelper.toHex(instance.digest()));
+ assertArrayEquals(KECCAK_512_SINGLE, StringHelper.toHex(instance.digest(SINGLE_SRC)));
+ assertArrayEquals(KECCAK_512_SHORT, StringHelper.toHex(instance.digest(SHORT_SRC)));
+ assertArrayEquals(KECCAK_512_LONG, StringHelper.toHex(instance.digest(LONG_SRC)));
+ }
+
+
+ /*private static final void showHashAlgorithms(Provider prov, Class> typeClass) {
+ String type = typeClass.getSimpleName();
+
+ List algos = new ArrayList<>();
+
+ Set services = prov.getServices();
+ for (Provider.Service service : services) {
+ if (service.getType().equalsIgnoreCase(type)) {
+ algos.add(service);
+ }
+ }
+
+ if (!algos.isEmpty()) {
+ System.out.printf(" --- Provider %s, version %.2f --- %n", prov.getName(), prov.getVersion());
+ for (Provider.Service service : algos) {
+ String algo = service.getAlgorithm();
+ System.out.printf("Algorithm name: \"%s\"%n", algo);
+
+
+ }
+ }
+
+ // --- find aliases (inefficiently)
+ Set