Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

View File

@ -0,0 +1,76 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Calculates the Adler32 of the given Bytes.
*/
class Adler32 {
var a1:Int;
var a2:Int;
public function new() {
a1 = 1;
a2 = 0;
}
public function get() {
return (a2 << 16) | a1;
}
public function update(b:haxe.io.Bytes, pos, len) {
var a1 = a1, a2 = a2;
for (p in pos...pos + len) {
var c = b.get(p);
a1 = (a1 + c) % 65521;
a2 = (a2 + a1) % 65521;
}
this.a1 = a1;
this.a2 = a2;
}
public function equals(a:Adler32) {
return a.a1 == a1 && a.a2 == a2;
}
public function toString() {
return StringTools.hex(a2, 8) + StringTools.hex(a1, 8);
}
public static function read(i:haxe.io.Input) {
var a = new Adler32();
var a2a = i.readByte();
var a2b = i.readByte();
var a1a = i.readByte();
var a1b = i.readByte();
a.a1 = (a1a << 8) | a1b;
a.a2 = (a2a << 8) | a2b;
return a;
}
public static function make(b:haxe.io.Bytes) {
var a = new Adler32();
a.update(b, 0, b.length);
return a.get();
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Allows one to encode/decode String and bytes using Base64 encoding.
**/
class Base64 {
public static var CHARS(default, null) = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static var BYTES(default, null) = haxe.io.Bytes.ofString(CHARS);
public static var URL_CHARS(default, null) = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
public static var URL_BYTES(default, null) = haxe.io.Bytes.ofString(URL_CHARS);
public static function encode(bytes:haxe.io.Bytes, complement = true):String {
var str = new BaseCode(BYTES).encodeBytes(bytes).toString();
if (complement)
switch (bytes.length % 3) {
case 1:
str += "==";
case 2:
str += "=";
default:
}
return str;
}
public static function decode(str:String, complement = true):haxe.io.Bytes {
if (complement)
while (str.charCodeAt(str.length - 1) == "=".code)
str = str.substr(0, -1);
return new BaseCode(BYTES).decodeBytes(haxe.io.Bytes.ofString(str));
}
public static function urlEncode(bytes:haxe.io.Bytes, complement = false):String {
var str = new BaseCode(URL_BYTES).encodeBytes(bytes).toString();
if (complement)
switch (bytes.length % 3) {
case 1:
str += "==";
case 2:
str += "=";
default:
}
return str;
}
public static function urlDecode(str:String, complement = false):haxe.io.Bytes {
if (complement)
while (str.charCodeAt(str.length - 1) == "=".code)
str = str.substr(0, -1);
return new BaseCode(URL_BYTES).decodeBytes(haxe.io.Bytes.ofString(str));
}
}

View File

@ -0,0 +1,142 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Allows one to encode/decode String and bytes using a power of two base dictionary.
**/
class BaseCode {
var base:haxe.io.Bytes;
var nbits:Int;
var tbl:Array<Int>;
public function new(base:haxe.io.Bytes) {
var len = base.length;
var nbits = 1;
while (len > 1 << nbits)
nbits++;
if (nbits > 8 || len != 1 << nbits)
throw "BaseCode : base length must be a power of two.";
this.base = base;
this.nbits = nbits;
}
public function encodeBytes(b:haxe.io.Bytes):haxe.io.Bytes {
#if (neko && !interp)
return haxe.io.Bytes.ofData(base_encode(b.getData(), base.getData()));
#else
var nbits = this.nbits;
var base = this.base;
var size = Std.int(b.length * 8 / nbits);
var out = haxe.io.Bytes.alloc(size + (((b.length * 8) % nbits == 0) ? 0 : 1));
var buf = 0;
var curbits = 0;
var mask = (1 << nbits) - 1;
var pin = 0;
var pout = 0;
while (pout < size) {
while (curbits < nbits) {
curbits += 8;
buf <<= 8;
buf |= b.get(pin++);
}
curbits -= nbits;
out.set(pout++, base.get((buf >> curbits) & mask));
}
if (curbits > 0)
out.set(pout++, base.get((buf << (nbits - curbits)) & mask));
return out;
#end
}
function initTable() {
var tbl = new Array();
for (i in 0...256)
tbl[i] = -1;
for (i in 0...base.length)
tbl[base.get(i)] = i;
this.tbl = tbl;
}
public function decodeBytes(b:haxe.io.Bytes):haxe.io.Bytes {
#if (neko && !interp)
return haxe.io.Bytes.ofData(base_decode(b.getData(), base.getData()));
#else
var nbits = this.nbits;
var base = this.base;
if (this.tbl == null)
initTable();
var tbl = this.tbl;
var size = (b.length * nbits) >> 3;
var out = haxe.io.Bytes.alloc(size);
var buf = 0;
var curbits = 0;
var pin = 0;
var pout = 0;
while (pout < size) {
while (curbits < 8) {
curbits += nbits;
buf <<= nbits;
var i = tbl[b.get(pin++)];
if (i == -1)
throw "BaseCode : invalid encoded char";
buf |= i;
}
curbits -= 8;
out.set(pout++, (buf >> curbits) & 0xFF);
}
return out;
#end
}
public function encodeString(s:String) {
#if (neko && !interp)
return neko.NativeString.toString(base_encode(neko.NativeString.ofString(s), base.getData()));
#else
return encodeBytes(haxe.io.Bytes.ofString(s)).toString();
#end
}
public function decodeString(s:String) {
#if (neko && !interp)
return neko.NativeString.toString(base_decode(neko.NativeString.ofString(s), base.getData()));
#else
return decodeBytes(haxe.io.Bytes.ofString(s)).toString();
#end
}
public static function encode(s:String, base:String) {
var b = new BaseCode(haxe.io.Bytes.ofString(base));
return b.encodeString(s);
}
public static function decode(s:String, base:String) {
var b = new BaseCode(haxe.io.Bytes.ofString(base));
return b.decodeString(s);
}
#if neko
private static var base_encode = neko.Lib.load("std", "base_encode", 2);
private static var base_decode = neko.Lib.load("std", "base_decode", 2);
#end
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Calculates the Crc32 of the given Bytes.
*/
class Crc32 {
var crc:Int;
public inline function new() {
crc = 0xFFFFFFFF;
}
public inline function byte(b:Int) {
var tmp = (crc ^ b) & 0xFF;
for (j in 0...8)
tmp = (tmp >>> 1) ^ (-(tmp & 1) & 0xEDB88320);
crc = (crc >>> 8) ^ tmp;
}
public inline function update(b:haxe.io.Bytes, pos, len) {
var b = b.getData();
for (i in pos...pos + len) {
var tmp = (crc ^ haxe.io.Bytes.fastGet(b, i)) & 0xFF;
for (j in 0...8)
tmp = (tmp >>> 1) ^ (-(tmp & 1) & 0xEDB88320);
crc = (crc >>> 8) ^ tmp;
}
}
public inline function get() {
return crc ^ 0xFFFFFFFF;
}
/**
Calculates the CRC32 of the given data bytes
**/
public static function make(data:haxe.io.Bytes):Int {
var c = new Crc32();
c.update(data, 0, data.length);
return c.get();
}
}

View File

@ -0,0 +1,90 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Hash methods for Hmac calculation.
**/
enum HashMethod {
MD5;
SHA1;
SHA256;
}
/**
Calculates a Hmac of the given Bytes using a HashMethod.
**/
class Hmac {
var method:HashMethod;
var blockSize:Int;
var length:Int;
public function new(hashMethod:HashMethod) {
method = hashMethod;
blockSize = switch (hashMethod) {
case MD5, SHA1, SHA256: 64;
}
length = switch (hashMethod) {
case MD5: 16;
case SHA1: 20;
case SHA256: 32;
}
}
inline function doHash(b:haxe.io.Bytes):haxe.io.Bytes {
return switch (method) {
case MD5: Md5.make(b);
case SHA1: Sha1.make(b);
case SHA256: Sha256.make(b);
}
}
function nullPad(s:haxe.io.Bytes, chunkLen:Int):haxe.io.Bytes {
var r = chunkLen - (s.length % chunkLen);
if (r == chunkLen && s.length != 0)
return s;
var sb = new haxe.io.BytesBuffer();
sb.add(s);
for (x in 0...r)
sb.addByte(0);
return sb.getBytes();
}
public function make(key:haxe.io.Bytes, msg:haxe.io.Bytes):haxe.io.Bytes {
if (key.length > blockSize) {
key = doHash(key);
}
key = nullPad(key, blockSize);
var Ki = new haxe.io.BytesBuffer();
var Ko = new haxe.io.BytesBuffer();
for (i in 0...key.length) {
Ko.addByte(key.get(i) ^ 0x5c);
Ki.addByte(key.get(i) ^ 0x36);
}
// hash(Ko + hash(Ki + message))
Ki.add(msg);
Ko.add(doHash(Ki.getBytes()));
return doHash(Ko.getBytes());
}
}

View File

@ -0,0 +1,268 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Creates a MD5 of a String.
**/
class Md5 {
public static function encode(s:String):String {
var m = new Md5();
var h = m.doEncode(str2blks(s));
return m.hex(h);
}
public static function make(b:haxe.io.Bytes):haxe.io.Bytes {
var h = new Md5().doEncode(bytes2blks(b));
var out = haxe.io.Bytes.alloc(16);
var p = 0;
for (i in 0...4) {
out.set(p++, h[i] & 0xFF);
out.set(p++, (h[i] >> 8) & 0xFF);
out.set(p++, (h[i] >> 16) & 0xFF);
out.set(p++, h[i] >>> 24);
}
return out;
}
/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Copyright (C) Paul Johnston 1999 - 2000.
* Updated by Greg Holt 2000 - 2001.
* See http://pajhome.org.uk/site/legal.html for details.
*/
function new() {}
function bitOR(a, b) {
var lsb = (a & 0x1) | (b & 0x1);
var msb31 = (a >>> 1) | (b >>> 1);
return (msb31 << 1) | lsb;
}
function bitXOR(a, b) {
var lsb = (a & 0x1) ^ (b & 0x1);
var msb31 = (a >>> 1) ^ (b >>> 1);
return (msb31 << 1) | lsb;
}
function bitAND(a, b) {
var lsb = (a & 0x1) & (b & 0x1);
var msb31 = (a >>> 1) & (b >>> 1);
return (msb31 << 1) | lsb;
}
function addme(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function hex(a:Array<Int>) {
var str = "";
var hex_chr = "0123456789abcdef";
for (num in a)
for (j in 0...4)
str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
return str;
}
static function bytes2blks(b:haxe.io.Bytes) {
var nblk = ((b.length + 8) >> 6) + 1;
var blks = new Array();
// preallocate size
var blksSize = nblk * 16;
#if (neko || cs || cpp || java || hl)
blks[blksSize - 1] = 0;
#end
#if !(cpp || cs || hl) // C++ and C# will already initialize them with zeroes.
for (i in 0...blksSize)
blks[i] = 0;
#end
var i = 0;
while (i < b.length) {
blks[i >> 2] |= b.get(i) << ((((b.length << 3) + i) & 3) << 3);
i++;
}
blks[i >> 2] |= 0x80 << (((b.length * 8 + i) % 4) * 8);
var l = b.length * 8;
var k = nblk * 16 - 2;
blks[k] = (l & 0xFF);
blks[k] |= ((l >>> 8) & 0xFF) << 8;
blks[k] |= ((l >>> 16) & 0xFF) << 16;
blks[k] |= ((l >>> 24) & 0xFF) << 24;
return blks;
}
static function str2blks(str:String) {
#if target.unicode
var str = haxe.io.Bytes.ofString(str);
#end
var nblk = ((str.length + 8) >> 6) + 1;
var blks = new Array();
// preallocate size
var blksSize = nblk * 16;
#if (neko || eval || cs || cpp || java || hl)
blks[blksSize - 1] = 0;
#end
#if !(cpp || cs || hl) // C++ and C# will already initialize them with zeroes.
for (i in 0...blksSize)
blks[i] = 0;
#end
var i = 0;
var max = str.length;
var l = max * 8;
while (i < max) {
blks[i >> 2] |= #if target.unicode str.get(i) #else StringTools.fastCodeAt(str, i) #end << (((l + i) % 4) * 8);
i++;
}
blks[i >> 2] |= 0x80 << (((l + i) % 4) * 8);
var k = nblk * 16 - 2;
blks[k] = (l & 0xFF);
blks[k] |= ((l >>> 8) & 0xFF) << 8;
blks[k] |= ((l >>> 16) & 0xFF) << 16;
blks[k] |= ((l >>> 24) & 0xFF) << 24;
return blks;
}
function rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
function cmn(q, a, b, x, s, t) {
return addme(rol((addme(addme(a, q), addme(x, t))), s), b);
}
function ff(a, b, c, d, x, s, t) {
return cmn(bitOR(bitAND(b, c), bitAND((~b), d)), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn(bitOR(bitAND(b, d), bitAND(c, (~d))), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(bitXOR(bitXOR(b, c), d), a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(bitXOR(c, bitOR(b, (~d))), a, b, x, s, t);
}
function doEncode(x:Array<Int>):Array<Int> {
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var step;
var i = 0;
while (i < x.length) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
step = 0;
a = ff(a, b, c, d, x[i + 0], 7, -680876936);
d = ff(d, a, b, c, x[i + 1], 12, -389564586);
c = ff(c, d, a, b, x[i + 2], 17, 606105819);
b = ff(b, c, d, a, x[i + 3], 22, -1044525330);
a = ff(a, b, c, d, x[i + 4], 7, -176418897);
d = ff(d, a, b, c, x[i + 5], 12, 1200080426);
c = ff(c, d, a, b, x[i + 6], 17, -1473231341);
b = ff(b, c, d, a, x[i + 7], 22, -45705983);
a = ff(a, b, c, d, x[i + 8], 7, 1770035416);
d = ff(d, a, b, c, x[i + 9], 12, -1958414417);
c = ff(c, d, a, b, x[i + 10], 17, -42063);
b = ff(b, c, d, a, x[i + 11], 22, -1990404162);
a = ff(a, b, c, d, x[i + 12], 7, 1804603682);
d = ff(d, a, b, c, x[i + 13], 12, -40341101);
c = ff(c, d, a, b, x[i + 14], 17, -1502002290);
b = ff(b, c, d, a, x[i + 15], 22, 1236535329);
a = gg(a, b, c, d, x[i + 1], 5, -165796510);
d = gg(d, a, b, c, x[i + 6], 9, -1069501632);
c = gg(c, d, a, b, x[i + 11], 14, 643717713);
b = gg(b, c, d, a, x[i + 0], 20, -373897302);
a = gg(a, b, c, d, x[i + 5], 5, -701558691);
d = gg(d, a, b, c, x[i + 10], 9, 38016083);
c = gg(c, d, a, b, x[i + 15], 14, -660478335);
b = gg(b, c, d, a, x[i + 4], 20, -405537848);
a = gg(a, b, c, d, x[i + 9], 5, 568446438);
d = gg(d, a, b, c, x[i + 14], 9, -1019803690);
c = gg(c, d, a, b, x[i + 3], 14, -187363961);
b = gg(b, c, d, a, x[i + 8], 20, 1163531501);
a = gg(a, b, c, d, x[i + 13], 5, -1444681467);
d = gg(d, a, b, c, x[i + 2], 9, -51403784);
c = gg(c, d, a, b, x[i + 7], 14, 1735328473);
b = gg(b, c, d, a, x[i + 12], 20, -1926607734);
a = hh(a, b, c, d, x[i + 5], 4, -378558);
d = hh(d, a, b, c, x[i + 8], 11, -2022574463);
c = hh(c, d, a, b, x[i + 11], 16, 1839030562);
b = hh(b, c, d, a, x[i + 14], 23, -35309556);
a = hh(a, b, c, d, x[i + 1], 4, -1530992060);
d = hh(d, a, b, c, x[i + 4], 11, 1272893353);
c = hh(c, d, a, b, x[i + 7], 16, -155497632);
b = hh(b, c, d, a, x[i + 10], 23, -1094730640);
a = hh(a, b, c, d, x[i + 13], 4, 681279174);
d = hh(d, a, b, c, x[i + 0], 11, -358537222);
c = hh(c, d, a, b, x[i + 3], 16, -722521979);
b = hh(b, c, d, a, x[i + 6], 23, 76029189);
a = hh(a, b, c, d, x[i + 9], 4, -640364487);
d = hh(d, a, b, c, x[i + 12], 11, -421815835);
c = hh(c, d, a, b, x[i + 15], 16, 530742520);
b = hh(b, c, d, a, x[i + 2], 23, -995338651);
a = ii(a, b, c, d, x[i + 0], 6, -198630844);
d = ii(d, a, b, c, x[i + 7], 10, 1126891415);
c = ii(c, d, a, b, x[i + 14], 15, -1416354905);
b = ii(b, c, d, a, x[i + 5], 21, -57434055);
a = ii(a, b, c, d, x[i + 12], 6, 1700485571);
d = ii(d, a, b, c, x[i + 3], 10, -1894986606);
c = ii(c, d, a, b, x[i + 10], 15, -1051523);
b = ii(b, c, d, a, x[i + 1], 21, -2054922799);
a = ii(a, b, c, d, x[i + 8], 6, 1873313359);
d = ii(d, a, b, c, x[i + 15], 10, -30611744);
c = ii(c, d, a, b, x[i + 6], 15, -1560198380);
b = ii(b, c, d, a, x[i + 13], 21, 1309151649);
a = ii(a, b, c, d, x[i + 4], 6, -145523070);
d = ii(d, a, b, c, x[i + 11], 10, -1120210379);
c = ii(c, d, a, b, x[i + 2], 15, 718787259);
b = ii(b, c, d, a, x[i + 9], 21, -343485551);
a = addme(a, olda);
b = addme(b, oldb);
c = addme(c, oldc);
d = addme(d, oldd);
i += 16;
}
return [a, b, c, d];
}
}

View File

@ -0,0 +1,172 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Creates a Sha1 of a String.
**/
class Sha1 {
public static function encode(s:String):String {
var sh = new Sha1();
var h = sh.doEncode(str2blks(s));
return sh.hex(h);
}
public static function make(b:haxe.io.Bytes):haxe.io.Bytes {
var h = new Sha1().doEncode(bytes2blks(b));
var out = haxe.io.Bytes.alloc(20);
var p = 0;
for (i in 0...5) {
out.set(p++, h[i] >>> 24);
out.set(p++, (h[i] >> 16) & 0xFF);
out.set(p++, (h[i] >> 8) & 0xFF);
out.set(p++, h[i] & 0xFF);
}
return out;
}
function new() {}
function doEncode(x:Array<Int>):Array<Int> {
var w = new Array<Int>();
var a = 0x67452301;
var b = 0xEFCDAB89;
var c = 0x98BADCFE;
var d = 0x10325476;
var e = 0xC3D2E1F0;
var i = 0;
while (i < x.length) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
var j = 0;
while (j < 80) {
if (j < 16)
w[j] = x[i + j];
else
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = rol(a, 5) + ft(j, b, c, d) + e + w[j] + kt(j);
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
j++;
}
a += olda;
b += oldb;
c += oldc;
d += oldd;
e += olde;
i += 16;
}
return [a, b, c, d, e];
}
/**
Convert a string to a sequence of 16-word blocks, stored as an array.
Append padding bits and the length, as described in the SHA1 standard.
**/
static function str2blks(s:String):Array<Int> {
#if target.unicode
var s = haxe.io.Bytes.ofString(s);
#end
var nblk = ((s.length + 8) >> 6) + 1;
var blks = new Array<Int>();
for (i in 0...nblk * 16)
blks[i] = 0;
for (i in 0...s.length) {
var p = i >> 2;
blks[p] |= #if target.unicode s.get(i) #else StringTools.fastCodeAt(s, i) #end << (24 - ((i & 3) << 3));
}
var i = s.length;
var p = i >> 2;
blks[p] |= 0x80 << (24 - ((i & 3) << 3));
blks[nblk * 16 - 1] = s.length * 8;
return blks;
}
static function bytes2blks(b:haxe.io.Bytes):Array<Int> {
var nblk = ((b.length + 8) >> 6) + 1;
var blks = new Array<Int>();
for (i in 0...nblk * 16)
blks[i] = 0;
for (i in 0...b.length) {
var p = i >> 2;
blks[p] |= b.get(i) << (24 - ((i & 3) << 3));
}
var i = b.length;
var p = i >> 2;
blks[p] |= 0x80 << (24 - ((i & 3) << 3));
blks[nblk * 16 - 1] = b.length * 8;
return blks;
}
/**
Bitwise rotate a 32-bit number to the left
**/
inline function rol(num:Int, cnt:Int):Int {
return (num << cnt) | (num >>> (32 - cnt));
}
/**
Perform the appropriate triplet combination function for the current iteration
**/
function ft(t:Int, b:Int, c:Int, d:Int):Int {
if (t < 20)
return (b & c) | ((~b) & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
/**
Determine the appropriate additive constant for the current iteration
**/
function kt(t:Int):Int {
if (t < 20)
return 0x5A827999;
if (t < 40)
return 0x6ED9EBA1;
if (t < 60)
return 0x8F1BBCDC;
return 0xCA62C1D6;
}
function hex(a:Array<Int>) {
var str = "";
for (num in a) {
str += StringTools.hex(num, 8);
}
return str.toLowerCase();
}
}

View File

@ -0,0 +1,191 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of Charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERChANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Creates a Sha224 of a String.
**/
class Sha224 {
public static function encode(s:String):String {
var sh = new Sha224();
var h = sh.doEncode(s, s.length * 8);
return sh.hex(h);
}
public static function make(b:haxe.io.Bytes):haxe.io.Bytes {
var h = new Sha224().doEncode(b.toString(), b.length * 8);
var out = haxe.io.Bytes.alloc(28);
var p = 0;
for (i in 0...8) {
out.set(p++, h[i] >>> 24);
out.set(p++, (h[i] >> 16) & 0xFF);
out.set(p++, (h[i] >> 8) & 0xFF);
out.set(p++, h[i] & 0xFF);
}
return out;
}
public function new() {}
function doEncode(str:String, strlen:Int):Array<Int> {
var K:Array<Int> = [
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC,
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967,
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
];
var HASH:Array<Int> = [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
];
var W = new Array<Int>();
W[64] = 0;
var a:Int, b:Int, c:Int, d:Int, e:Int, f:Int, g:Int, h:Int, i:Int, j:Int;
var T1, T2;
var i:Int = 0;
var blocks:Array<Int> = str2blks(str);
blocks[strlen >> 5] |= 0x80 << (24 - strlen % 32);
blocks[((strlen + 64 >> 9) << 4) + 15] = strlen;
while (i < blocks.length) {
a = HASH[0];
b = HASH[1];
c = HASH[2];
d = HASH[3];
e = HASH[4];
f = HASH[5];
g = HASH[6];
h = HASH[7];
for (j in 0...64) {
if (j < 16) {
W[j] = blocks[j + i];
} else {
W[j] = safeAdd(safeAdd(safeAdd(Gamma1(W[j - 2]), W[j - 7]), Gamma0(W[j - 15])), W[j - 16]);
}
T1 = safeAdd(safeAdd(safeAdd(safeAdd(h, Sigma1(e)), Ch(e, f, g)), K[j]), W[j]);
T2 = safeAdd(Sigma0(a), Maj(a, b, c));
h = g;
g = f;
f = e;
e = safeAdd(d, T1);
d = c;
c = b;
b = a;
a = safeAdd(T1, T2);
}
HASH[0] = safeAdd(a, HASH[0]);
HASH[1] = safeAdd(b, HASH[1]);
HASH[2] = safeAdd(c, HASH[2]);
HASH[3] = safeAdd(d, HASH[3]);
HASH[4] = safeAdd(e, HASH[4]);
HASH[5] = safeAdd(f, HASH[5]);
HASH[6] = safeAdd(g, HASH[6]);
HASH[7] = safeAdd(h, HASH[7]);
i += 16;
}
return HASH;
}
static function str2blks(s:String):Array<Int> {
var nblk = ((s.length + 8) >> 6) + 1;
var blks = new Array<Int>();
for (i in 0...nblk * 16)
blks[i] = 0;
for (i in 0...s.length) {
var p = i >> 2;
blks[p] |= s.charCodeAt(i) << (24 - ((i & 3) << 3));
}
var i = s.length;
var p = i >> 2;
blks[p] |= 0x80 << (24 - ((i & 3) << 3));
blks[nblk * 16 - 1] = s.length * 8;
return blks;
}
extern inline static function safeAdd(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >>> 16) + (y >>> 16) + (lsw >>> 16);
return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
}
// ++
extern inline function ROTR(X, n) {
return (X >>> n) | (X << (32 - n));
}
// ++
extern inline function SHR(X, n) {
return (X >>> n);
}
// ++
extern inline function Ch(x, y, z) {
return ((x & y) ^ ((~x) & z));
}
// ++
extern inline function Maj(x, y, z) {
return ((x & y) ^ (x & z) ^ (y & z));
}
extern inline function Sigma0(x) {
return ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22);
}
extern inline function Sigma1(x) {
return ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25);
}
extern inline function Gamma0(x) {
return ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3);
}
extern inline function Gamma1(x) {
return ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10);
}
function hex(a:Array<Int>) {
var str = "";
for (num in a) {
str += StringTools.hex(num, 8);
}
return str.substring(0, 56).toLowerCase();
}
}

View File

@ -0,0 +1,195 @@
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package haxe.crypto;
/**
Creates a Sha256 of a String.
**/
class Sha256 {
public static function encode(s:String):String {
var sh = new Sha256();
var h = sh.doEncode(str2blks(s), s.length * 8);
return sh.hex(h);
}
public static function make(b:haxe.io.Bytes):haxe.io.Bytes {
var h = new Sha256().doEncode(bytes2blks(b), b.length * 8);
var out = haxe.io.Bytes.alloc(32);
var p = 0;
for (i in 0...8) {
out.set(p++, h[i] >>> 24);
out.set(p++, (h[i] >> 16) & 0xFF);
out.set(p++, (h[i] >> 8) & 0xFF);
out.set(p++, h[i] & 0xFF);
}
return out;
}
function new() {}
function doEncode(m:Array<Int>, l:Int):Array<Int> {
var K:Array<Int> = [
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
];
var HASH:Array<Int> = [
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
];
var W = new Array<Int>();
W[64] = 0;
var a:Int, b:Int, c:Int, d:Int, e:Int, f:Int, g:Int, h:Int;
var T1, T2;
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >> 9) << 4) + 15] = l;
var i:Int = 0;
while (i < m.length) {
a = HASH[0];
b = HASH[1];
c = HASH[2];
d = HASH[3];
e = HASH[4];
f = HASH[5];
g = HASH[6];
h = HASH[7];
for (j in 0...64) {
if (j < 16)
W[j] = m[j + i];
else
W[j] = safeAdd(safeAdd(safeAdd(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
T1 = safeAdd(safeAdd(safeAdd(safeAdd(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
T2 = safeAdd(Sigma0256(a), Maj(a, b, c));
h = g;
g = f;
f = e;
e = safeAdd(d, T1);
d = c;
c = b;
b = a;
a = safeAdd(T1, T2);
}
HASH[0] = safeAdd(a, HASH[0]);
HASH[1] = safeAdd(b, HASH[1]);
HASH[2] = safeAdd(c, HASH[2]);
HASH[3] = safeAdd(d, HASH[3]);
HASH[4] = safeAdd(e, HASH[4]);
HASH[5] = safeAdd(f, HASH[5]);
HASH[6] = safeAdd(g, HASH[6]);
HASH[7] = safeAdd(h, HASH[7]);
i += 16;
}
return HASH;
}
/**
Convert a string to a sequence of 16-word blocks, stored as an array.
Append padding bits and the length, as described in the SHA1 standard.
**/
static function str2blks(s:String):Array<Int> {
#if target.unicode
var s = haxe.io.Bytes.ofString(s);
#end
var nblk = ((s.length + 8) >> 6) + 1;
var blks = new Array<Int>();
for (i in 0...nblk * 16)
blks[i] = 0;
for (i in 0...s.length) {
var p = i >> 2;
blks[p] |= #if target.unicode s.get(i) #else s.charCodeAt(i) #end << (24 - ((i & 3) << 3));
}
var i = s.length;
var p = i >> 2;
blks[p] |= 0x80 << (24 - ((i & 3) << 3));
blks[nblk * 16 - 1] = s.length * 8;
return blks;
}
static function bytes2blks(b:haxe.io.Bytes):Array<Int> {
var nblk = ((b.length + 8) >> 6) + 1;
var blks = new Array<Int>();
for (i in 0...nblk * 16)
blks[i] = 0;
for (i in 0...b.length) {
var p = i >> 2;
blks[p] |= b.get(i) << (24 - ((i & 3) << 3));
}
var i = b.length;
var p = i >> 2;
blks[p] |= 0x80 << (24 - ((i & 3) << 3));
blks[nblk * 16 - 1] = b.length * 8;
return blks;
}
extern inline function S(X, n) {
return (X >>> n) | (X << (32 - n));
}
extern inline function R(X, n) {
return (X >>> n);
}
extern inline function Ch(x, y, z) {
return ((x & y) ^ ((~x) & z));
}
extern inline function Maj(x, y, z) {
return ((x & y) ^ (x & z) ^ (y & z));
}
extern inline function Sigma0256(x) {
return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
}
extern inline function Sigma1256(x) {
return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
}
extern inline function Gamma0256(x) {
return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
}
extern inline function Gamma1256(x) {
return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
}
extern inline function safeAdd(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
function hex(a:Array<Int>) {
var str = "";
for (num in a) {
str += StringTools.hex(num, 8);
}
return str.toLowerCase();
}
}