1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Scribunto: Add HashLibrary [#589]

This commit is contained in:
gnosygnu
2019-10-12 15:07:21 -04:00
parent 9f0cfc27bb
commit 866debd51d
58 changed files with 1610 additions and 253 deletions

View File

@@ -0,0 +1,117 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos; import gplx.*; import gplx.core.*; import gplx.core.security.*;
import gplx.core.security.algos.bouncy_castle.*;
import gplx.core.security.algos.getopt.*;
import gplx.core.security.algos.gnu_crypto.*;
import gplx.core.security.algos.jre.*;
import gplx.core.security.algos.jacksum.*;
public class Hash_algo_factory__php_ {
public static Hash_algo_factory__composite New() {
Hash_algo_factory__composite rv = new Hash_algo_factory__composite();
rv.Reg_many(Bouncy_castle_factory.Instance
, Bouncy_castle_factory.Key__md2, Bouncy_castle_factory.Key__md4, Bouncy_castle_factory.Key__md5
, Bouncy_castle_factory.Key__sha1, Bouncy_castle_factory.Key__sha224, Bouncy_castle_factory.Key__sha256, Bouncy_castle_factory.Key__sha384
);
rv.Reg_one(Bouncy_castle_factory.Instance, Bouncy_castle_factory.Key__sha_512_224, "sha512/224");
rv.Reg_one(Bouncy_castle_factory.Instance, Bouncy_castle_factory.Key__sha_512_256, "sha512/256");
rv.Reg_many(Bouncy_castle_factory.Instance
, Bouncy_castle_factory.Key__sha512
, Bouncy_castle_factory.Key__sha3_224, Bouncy_castle_factory.Key__sha3_256, Bouncy_castle_factory.Key__sha3_384, Bouncy_castle_factory.Key__sha3_512
, Bouncy_castle_factory.Key__ripemd128, Bouncy_castle_factory.Key__ripemd160, Bouncy_castle_factory.Key__ripemd256, Bouncy_castle_factory.Key__ripemd320
, Bouncy_castle_factory.Key__whirlpool
);
rv.Reg_one(Jacksum_factory.Instance, Jacksum_factory.Key__tiger_128, "tiger128,3");
rv.Reg_one(Jacksum_factory.Instance, Jacksum_factory.Key__tiger_160, "tiger160,3");
rv.Reg_one(Jacksum_factory.Instance, Jacksum_factory.Key__tiger_192, "tiger192,3");
rv.Reg_many(Jacksum_factory.Instance, Jacksum_factory.Key__gost);
rv.Reg_many(Jre_checksum_factory.Instance, Jre_checksum_factory.Key__adler32);
// "crc" may be BZ2 CRC; https://stackoverflow.com/questions/40741707/php-hashcrc32-and-crc32-return-different-value
rv.Reg_one(Jre_checksum_factory.Instance, Jre_checksum_factory.Key__crc32, "crc32b"); // PHP crc32b is the equivalent of Java CRC
rv.Reg_many(Getopt_factory.Instance
, Getopt_factory.Key__fnv132, Getopt_factory.Key__fnv164, Getopt_factory.Key__fnv1a32, Getopt_factory.Key__fnv1a64
);
rv.Reg_many(Gnu_haval_factory.Instance
, Gnu_haval_factory.Key__haval128_3, Gnu_haval_factory.Key__haval160_3, Gnu_haval_factory.Key__haval192_3, Gnu_haval_factory.Key__haval224_3, Gnu_haval_factory.Key__haval256_3
, Gnu_haval_factory.Key__haval128_4, Gnu_haval_factory.Key__haval160_4, Gnu_haval_factory.Key__haval192_4, Gnu_haval_factory.Key__haval224_4, Gnu_haval_factory.Key__haval256_4
, Gnu_haval_factory.Key__haval128_5, Gnu_haval_factory.Key__haval160_5, Gnu_haval_factory.Key__haval192_5, Gnu_haval_factory.Key__haval224_5, Gnu_haval_factory.Key__haval256_5
);
return rv;
}
}
/*
URL: https://en.wikipedia.org/w/index.php?title=Module:Sandbox/Gnosygnu&action=edit
SRC: mw.logObject(mw.hash.listAlgorithms())
OUT:
table#1 {
"md2", -- bouncycastle
"md4", -- bouncycastle
"md5", -- bouncycastle
"sha1", -- bouncycastle
"sha224", -- bouncycastle
"sha256", -- bouncycastle
"sha384", -- bouncycastle
"sha512/224", -- bouncycastle
"sha512/256", -- bouncycastle
"sha512", -- bouncycastle
"sha3-224", -- bouncycastle
"sha3-256", -- bouncycastle
"sha3-384", -- bouncycastle
"sha3-512", -- bouncycastle
"ripemd128", -- bouncycastle
"ripemd160", -- bouncycastle
"ripemd256", -- bouncycastle
"ripemd320", -- bouncycastle
"whirlpool", -- bouncycastle
"tiger128,3", -- jacksum
"tiger160,3", -- jacksum
"tiger192,3", -- jacksum
"tiger128,4",
"tiger160,4",
"tiger192,4",
"snefru",
"snefru256",
"gost", -- jacksum; not in bouncycastle (tried GOST3411; GOST3411-2012-256; GOST3411-2012-512)
"gost-crypto",
"adler32", -- jre
"crc32",
"crc32b", -- jre
"fnv132", -- getopt
"fnv1a32", -- getopt
"fnv164", -- getopt
"fnv1a64", -- getopt
"joaat",
"haval128,3", -- gnu-crypto
"haval160,3", -- gnu-crypto
"haval192,3", -- gnu-crypto
"haval224,3", -- gnu-crypto
"haval256,3", -- gnu-crypto
"haval128,4", -- gnu-crypto
"haval160,4", -- gnu-crypto
"haval192,4", -- gnu-crypto
"haval224,4", -- gnu-crypto
"haval256,4", -- gnu-crypto
"haval128,5", -- gnu-crypto
"haval160,5", -- gnu-crypto
"haval192,5", -- gnu-crypto
"haval224,5", -- gnu-crypto
"haval256,5", -- gnu-crypto
}
REF:
* https://www.bouncycastle.org/specifications.html
* https://www.gnu.org/software/gnu-crypto/manual/api/gnu/crypto/hash/BaseHash.html
*/

View File

@@ -0,0 +1,48 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.bouncy_castle; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import gplx.core.encoders.*;
import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Bouncy_castle_algo implements Hash_algo {
private static boolean Provider_needs_init = true;
private final MessageDigest md;
public Bouncy_castle_algo(String key) {
// register BounceCastleProvider
if (Provider_needs_init) {
Provider_needs_init = false;
Security.addProvider(new BouncyCastleProvider());
}
// get digest
try {
this.key = key;
this.md = MessageDigest.getInstance(key);
}
catch (Exception exc) {
throw Err_.new_wo_type("unknown messageDigest; key=" + key);
}
}
public String Key() {return key;} private final String key;
public Hash_algo Clone_hash_algo() {return new Bouncy_castle_algo(key);}
public void Update_digest(byte[] bry, int bgn, int end) {md.update(bry, bgn, end - bgn);}
public byte[] To_hash_bry() {
// get hash
byte[] hash = md.digest();
return Hex_utl_.Encode_bry(hash);
}
}

View File

@@ -0,0 +1,30 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.bouncy_castle; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
public class Bouncy_castle_factory implements Hash_algo_factory {
public Hash_algo New_hash_algo(String key) {
return new Bouncy_castle_algo(key);
}
public static String
Key__md2 = "md2", Key__md4 = "md4", Key__md5 = "md5"
, Key__sha1 = "sha1", Key__sha224 = "sha224", Key__sha256 = "sha256", Key__sha384 = "sha384"
, Key__sha_512_224 = "sha-512/224", Key__sha_512_256 = "sha-512/256", Key__sha512 = "sha512"
, Key__sha3_224 = "sha3-224", Key__sha3_256 = "sha3-256", Key__sha3_384 = "sha3-384", Key__sha3_512 = "sha3-512"
, Key__ripemd128 = "ripemd128", Key__ripemd160 = "ripemd160", Key__ripemd256 = "ripemd256", Key__ripemd320 = "ripemd320"
, Key__whirlpool = "whirlpool"
;
public static final Bouncy_castle_factory Instance = new Bouncy_castle_factory(); Bouncy_castle_factory() {}
}

View File

@@ -0,0 +1,38 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.getopt; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import org.getopt.util.hash.FNV1;
public class Getopt_algo implements Hash_algo {
private final Getopt_factory factory;
private final FNV1 hash;
private final int pad_min;
public Getopt_algo(Getopt_factory factory, String key) {
this.key = key;
this.factory = factory;
this.pad_min = String_.Has_at_end(key, "32") ? 7 : 15;
this.hash = factory.New_FNV1(key);
}
public String Key() {return key;} private final String key;
public Hash_algo Clone_hash_algo() {return new Getopt_algo(factory, key);}
public void Update_digest(byte[] bry, int bgn, int end) {hash.init(bry, bgn, end);}
public byte[] To_hash_bry() {
long val = hash.getHash();
String rv = Long.toHexString(val);
if (String_.Len(rv) == pad_min)
rv = "0" + rv;
return Bry_.new_u8(rv);
}
}

View File

@@ -0,0 +1,33 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.getopt; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import org.getopt.util.hash.*;
public class Getopt_factory implements Hash_algo_factory {
public Hash_algo New_hash_algo(String key) {
return new Getopt_algo(this, key);
}
public FNV1 New_FNV1(String key) {
if (String_.Eq(key, Key__fnv132)) return new FNV132();
else if (String_.Eq(key, Key__fnv164)) return new FNV164();
else if (String_.Eq(key, Key__fnv1a32)) return new FNV1a32();
else if (String_.Eq(key, Key__fnv1a64)) return new FNV1a64();
else throw Err_.new_unhandled(key);
}
public static String
Key__fnv132 = "fnv132", Key__fnv164 = "fnv164", Key__fnv1a32 = "fnv1a32", Key__fnv1a64 = "fnv1a64"
;
public static final Getopt_factory Instance = new Getopt_factory(); Getopt_factory() {}
}

View File

@@ -0,0 +1,34 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.gnu_crypto; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import gnu.crypto.hash.Haval;
import gnu.crypto.hash.Tiger;
public class Gnu_haval_algo implements Hash_algo {
private final Gnu_haval_factory factory;
private final Haval haval;
public Gnu_haval_algo(Gnu_haval_factory factory, String key) {
this.factory = factory;
this.key = key;
this.haval = factory.New_Haval(key);
}
public String Key() {return key;} private final String key;
public Hash_algo Clone_hash_algo() {return new Gnu_haval_algo(factory, key);}
public void Update_digest(byte[] bry, int bgn, int end) {haval.update(bry, bgn, end);}
public byte[] To_hash_bry() {
byte[] rv = haval.digest();
return gplx.core.encoders.Hex_utl_.Encode_bry(rv);
}
}

View File

@@ -0,0 +1,52 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.gnu_crypto; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import gnu.crypto.hash.Haval;
public class Gnu_haval_factory implements Hash_algo_factory {
public Hash_algo New_hash_algo(String key) {
return new Gnu_haval_algo(this, key);
}
public Haval New_Haval(String key) {
// parse key for size; EX: "128" in "haval128,3"
int size = Int_.Parse_or(String_.Mid(key, 5, 8), -1);
switch (size) {
case 128: size = Haval.HAVAL_128_BIT; break;
case 160: size = Haval.HAVAL_160_BIT; break;
case 192: size = Haval.HAVAL_192_BIT; break;
case 224: size = Haval.HAVAL_224_BIT; break;
case 256: size = Haval.HAVAL_256_BIT; break;
default: throw Err_.new_unhandled_default(size);
}
// parse key for round; EX: "3" in "haval128,3"
int round = Int_.Parse_or(String_.Mid(key, 9, 10), -1);
switch (round) {
case 3: round = Haval.HAVAL_3_ROUND; break;
case 4: round = Haval.HAVAL_4_ROUND; break;
case 5: round = Haval.HAVAL_5_ROUND; break;
default: throw Err_.new_unhandled_default(round);
}
return new Haval(size, round);
}
public static String
Key__haval128_3 = "haval128,3", Key__haval160_3 = "haval160,3", Key__haval192_3 = "haval192,3", Key__haval224_3 = "haval224,3", Key__haval256_3 = "haval256,3"
, Key__haval128_4 = "haval128,4", Key__haval160_4 = "haval160,4", Key__haval192_4 = "haval192,4", Key__haval224_4 = "haval224,4", Key__haval256_4 = "haval256,4"
, Key__haval128_5 = "haval128,5", Key__haval160_5 = "haval160,5", Key__haval192_5 = "haval192,5", Key__haval224_5 = "haval224,5", Key__haval256_5 = "haval256,5"
;
public static final Gnu_haval_factory Instance = new Gnu_haval_factory(); Gnu_haval_factory() {}
}

View File

@@ -0,0 +1,38 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.jacksum; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
import gplx.core.encoders.*;
import java.security.NoSuchAlgorithmException;
import jonelo.jacksum.JacksumAPI;
import jonelo.jacksum.algorithm.AbstractChecksum;
public class Jacksum_algo implements Hash_algo {
private final AbstractChecksum checksum;
public Jacksum_algo(String key) {
this.key = key;
try {
this.checksum = JacksumAPI.getChecksumInstance(key);
} catch (NoSuchAlgorithmException nsae) {
throw Err_.new_wo_type("jacksum algo doesn't exist: key=" + key);
}
}
public String Key() {return key;} private final String key;
public Hash_algo Clone_hash_algo() {return new Jacksum_algo(key);}
public void Update_digest(byte[] bry, int bgn, int end) {checksum.update(bry, bgn, end - bgn);}
public byte[] To_hash_bry() {
byte[] rv = checksum.getByteArray();
return gplx.core.encoders.Hex_utl_.Encode_bry(rv);
}
}

View File

@@ -0,0 +1,29 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.algos.jacksum; import gplx.*; import gplx.core.*; import gplx.core.security.*; import gplx.core.security.algos.*;
public class Jacksum_factory implements Hash_algo_factory {
public Hash_algo New_hash_algo(String key) {
return new Jacksum_algo(key);
}
public Object New_Checksum(String key) {
return null;
}
public static String // REF: /source/jonelo/jacksum/JacksumAPI.java
Key__tiger_128 = "tiger-128", Key__tiger_160 = "tiger-160", Key__tiger_192= "tiger-192", Key__gost = "gost"
;
public static final Jacksum_factory Instance = new Jacksum_factory(); Jacksum_factory() {}
}

View File

@@ -14,6 +14,7 @@ GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.files; import gplx.*; import gplx.core.*; import gplx.core.security.*;
import gplx.core.security.algos.*;
public class Cksum_list {
public Cksum_list(byte type, Cksum_itm[] itms, long itms_size) {
this.Type = type; this.Itms = itms; this.Itms_size = itms_size;

View File

@@ -15,6 +15,7 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.core.security.files; import gplx.*; import gplx.core.*; import gplx.core.security.*;
import org.junit.*; import gplx.core.tests.*;
import gplx.core.security.algos.*;
public class Cksum_list_tst {
private final Cksum_list_fxt fxt = new Cksum_list_fxt();
@Test public void Basic() {
@@ -40,7 +41,7 @@ class Cksum_list_fxt {
}
public Cksum_itm Make__itm(String hash, String file_name, long size) {return new Cksum_itm(Bry_.new_u8(hash), dir.GenSubFil(file_name), size);}
public Cksum_list_fxt Test__parse(String raw, long expd_size, Cksum_itm... expd_itms) {
Cksum_list actl_list = Cksum_list.Parse(gplx.core.security.Hash_algo_.Tid__md5, dir, Bry_.new_u8(raw));
Cksum_list actl_list = Cksum_list.Parse(Hash_algo_.Tid__md5, dir, Bry_.new_u8(raw));
Gftest.Eq__long(expd_size, actl_list.Itms_size);
Gftest.Eq__ary(expd_itms, actl_list.Itms);
return this;