2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

.Net 6 Upgrade

This commit is contained in:
2021-12-01 12:17:45 +03:00
parent 1166e93ba9
commit 530df018ec
164 changed files with 21247 additions and 21425 deletions

View File

@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Text;
namespace Esiur.Security.Integrity
{
namespace Esiur.Security.Integrity;
public class CRC16IBM
{
static UInt16[] table = {
public class CRC16IBM
{
static UInt16[] table = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
@ -41,42 +40,40 @@ namespace Esiur.Security.Integrity
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040};
public static ushort Compute(byte[] data)
public static ushort Compute(byte[] data)
{
return Compute(data, 0, (uint)data.Length);
}
public static ushort Compute(byte[] data, uint offset, uint length)
{
ushort crc = 0;// 0xffff;
ushort x;
for (var i = offset; i < length; i++)
{
return Compute(data, 0, (uint)data.Length);
x = (ushort)(crc ^ data[i]);
crc = (UInt16)((crc >> 8) ^ table[x & 0x00FF]);
}
return crc;
}
public static ushort Compute(byte[] data, uint offset, uint length)
public static ushort Compute2(byte[] data, uint offset, uint length)
{
ushort crc = 0;
for (var i = offset; i < length; i++)
{
ushort crc = 0;// 0xffff;
ushort x;
for (var i = offset; i < length; i++)
crc ^= data[i];
for (var j = 0; j < 8; j++)
{
x = (ushort)(crc ^ data[i]);
crc = (UInt16)((crc >> 8) ^ table[x & 0x00FF]);
var carry = crc & 0x1;
crc >>= 1;
if (carry == 1)
crc ^= 0xa001;
}
return crc;
}
public static ushort Compute2(byte[] data, uint offset, uint length)
{
ushort crc = 0;
for (var i = offset; i < length; i++)
{
crc ^= data[i];
for (var j = 0; j < 8; j++)
{
var carry = crc & 0x1;
crc >>= 1;
if (carry == 1)
crc ^= 0xa001;
}
}
return crc;
}
return crc;
}
}

View File

@ -2,12 +2,11 @@
using System.Collections.Generic;
using System.Text;
namespace Esiur.Security.Integrity
{
namespace Esiur.Security.Integrity;
public class CRC16ITU
{
static UInt16[] table =
public class CRC16ITU
{
static UInt16[] table =
{
0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF,
0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7,
@ -44,23 +43,22 @@ namespace Esiur.Security.Integrity
};
public static ushort Compute(byte[] data)
public static ushort Compute(byte[] data)
{
return Compute(data, 0, (uint)data.Length);
}
public static ushort Compute(byte[] data, uint offset, uint length)
{
ushort fcs = 0xffff; // initialization
while (length > 0)
{
return Compute(data, 0, (uint)data.Length);
fcs = (ushort)((fcs >> 8) ^ table[(fcs ^ data[offset++]) & 0xff]);
length--;
}
public static ushort Compute(byte[] data, uint offset, uint length)
{
ushort fcs = 0xffff; // initialization
while (length > 0)
{
fcs = (ushort)((fcs >> 8) ^ table[(fcs ^ data[offset++]) & 0xff]);
length--;
}
return (ushort)~fcs; // negated
}
return (ushort)~fcs; // negated
}
}

View File

@ -28,14 +28,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Security.Integrity
namespace Esiur.Security.Integrity;
public enum HashFunctionType
{
public enum HashFunctionType
{
MD5 = 0,
SHA1,
SHA256,
SHA384,
SHA512
}
MD5 = 0,
SHA1,
SHA256,
SHA384,
SHA512
}

View File

@ -2,25 +2,23 @@
using System.Collections.Generic;
using System.Text;
namespace Esiur.Security.Integrity
namespace Esiur.Security.Integrity;
public class NMEA0183
{
public class NMEA0183
public static byte Compute(string data)
{
public static byte Compute(string data)
{
return Compute(data, 0, (uint)data.Length);
}
public static byte Compute(string data, uint offset, uint length)
{
byte rt = 0;
var ends = offset + length;
for (int i = (int)offset; i < ends; i++)
rt ^= (byte)data[i];
return rt;
}
return Compute(data, 0, (uint)data.Length);
}
public static byte Compute(string data, uint offset, uint length)
{
byte rt = 0;
var ends = offset + length;
for (int i = (int)offset; i < ends; i++)
rt ^= (byte)data[i];
return rt;
}
}

View File

@ -3,35 +3,34 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Security.Integrity
namespace Esiur.Security.Integrity;
public static class SHA256
{
public static class SHA256
static uint RROT(uint n, int d)
{
return (n >> d) | (n << (32 - d));
}
static uint RROT(uint n, int d)
{
return (n >> d) | (n << (32 - d));
}
public static byte[] Compute(byte[] msg)
{
/*
Note 1: All variables are 32 bit unsigned integers and addition is calculated modulo 2^32
Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 ≤ i ≤ 63
Note 3: The compression function uses 8 working variables, a through h
Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
and when parsing message block data from bytes to words, for example,
the first word of the input message "abc" after padding is 0x61626380
*/
public static byte[] Compute(byte[] msg)
{
/*
Note 1: All variables are 32 bit unsigned integers and addition is calculated modulo 2^32
Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 ≤ i ≤ 63
Note 3: The compression function uses 8 working variables, a through h
Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
and when parsing message block data from bytes to words, for example,
the first word of the input message "abc" after padding is 0x61626380
*/
// Initialize hash values:
// (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
// Initialize hash values:
// (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
var hash = new uint[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
var hash = new uint[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
// Initialize array of round constants:
// (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
var k = new uint[] {
// Initialize array of round constants:
// (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
var k = new uint[] {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@ -43,115 +42,114 @@ namespace Esiur.Security.Integrity
// Pre-processing:
// begin with the original message of length L bits
ulong L = (ulong)msg.Length * 8;
// Pre-processing:
// begin with the original message of length L bits
ulong L = (ulong)msg.Length * 8;
// append a single '1' bit
// append K '0' bits, where K is the minimum number >= 0 such that L + 1 + K + 64 is a multiple of 512
// append a single '1' bit
// append K '0' bits, where K is the minimum number >= 0 such that L + 1 + K + 64 is a multiple of 512
var K = 512 - ((L + 1 + 64) % 512);
var K = 512 - ((L + 1 + 64) % 512);
if (K == 512)
K = 0;
if (K == 512)
K = 0;
var paddingLength = (K + 1) / 8;
var paddingBytes = new byte[paddingLength];
paddingBytes[0] = 0x80;
var paddingLength = (K + 1) / 8;
var paddingBytes = new byte[paddingLength];
paddingBytes[0] = 0x80;
var data = new BinaryList().AddUInt8Array(msg).AddUInt8Array(paddingBytes).AddUInt64(L).ToArray();
var data = new BinaryList().AddUInt8Array(msg).AddUInt8Array(paddingBytes).AddUInt64(L).ToArray();
// append L as a 64-bit big-endian integer, making the total post-processed length a multiple of 512 bits
// append L as a 64-bit big-endian integer, making the total post-processed length a multiple of 512 bits
// Process the message in successive 512-bit chunks:
// break message into 512-bit chunks
// for each chunk
// Process the message in successive 512-bit chunks:
// break message into 512-bit chunks
// for each chunk
for (var chunk = 0; chunk < data.Length; chunk += 64)
for (var chunk = 0; chunk < data.Length; chunk += 64)
{
// create a 64-entry message schedule array w[0..63] of 32-bit words
// (The initial values in w[0..63] don't matter, so many implementations zero them here)
// copy chunk into first 16 words w[0..15] of the message schedule array
var w = new uint[64];
for (var i = 0; i < 16; i++)
w[i] = data.GetUInt32((uint)(chunk + (i * 4)));
//for(var i = 16; i < 64; i++)
// w[i] = 0;
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
// for i from 16 to 63
// s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
// s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
// w[i] := w[i-16] + s0 + w[i-7] + s1
for (var i = 16; i < 64; i++)
{
// create a 64-entry message schedule array w[0..63] of 32-bit words
// (The initial values in w[0..63] don't matter, so many implementations zero them here)
// copy chunk into first 16 words w[0..15] of the message schedule array
var w = new uint[64];
for (var i = 0; i < 16; i++)
w[i] = data.GetUInt32((uint)(chunk + (i * 4)));
//for(var i = 16; i < 64; i++)
// w[i] = 0;
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
// for i from 16 to 63
// s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
// s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
// w[i] := w[i-16] + s0 + w[i-7] + s1
for (var i = 16; i < 64; i++)
{
var s0 = SHA256.RROT(w[i - 15], 7) ^ SHA256.RROT(w[i - 15], 18) ^ (w[i - 15] >> 3);
var s1 = SHA256.RROT(w[i - 2], 17) ^ SHA256.RROT(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
// Initialize working variables to current hash value:
var a = hash[0];
var b = hash[1];
var c = hash[2];
var d = hash[3];
var e = hash[4];
var f = hash[5];
var g = hash[6];
var h = hash[7];
// Compression function main loop:
for (var i = 0; i < 64; i++)
{
var S1 = SHA256.RROT(e, 6) ^ SHA256.RROT(e, 11) ^ SHA256.RROT(e, 25);
var ch = (e & f) ^ ((~e) & g);
var temp1 = h + S1 + ch + k[i] + w[i];
var S0 = SHA256.RROT(a, 2) ^ SHA256.RROT(a, 13) ^ SHA256.RROT(a, 22);
var maj = (a & b) ^ (a & c) ^ (b & c);
uint temp2 = S0 + maj;
h = g;
g = f;
f = e;
e = (d + temp1) >> 0;
d = c;
c = b;
b = a;
a = (temp1 + temp2) >> 0;
}
// Add the compressed chunk to the current hash value:
hash[0] = (hash[0] + a) >> 0;
hash[1] = (hash[1] + b) >> 0;
hash[2] = (hash[2] + c) >> 0;
hash[3] = (hash[3] + d) >> 0;
hash[4] = (hash[4] + e) >> 0;
hash[5] = (hash[5] + f) >> 0;
hash[6] = (hash[6] + g) >> 0;
hash[7] = (hash[7] + h) >> 0;
var s0 = SHA256.RROT(w[i - 15], 7) ^ SHA256.RROT(w[i - 15], 18) ^ (w[i - 15] >> 3);
var s1 = SHA256.RROT(w[i - 2], 17) ^ SHA256.RROT(w[i - 2], 19) ^ (w[i - 2] >> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
// Initialize working variables to current hash value:
var a = hash[0];
var b = hash[1];
var c = hash[2];
var d = hash[3];
var e = hash[4];
var f = hash[5];
var g = hash[6];
var h = hash[7];
// Compression function main loop:
for (var i = 0; i < 64; i++)
{
var S1 = SHA256.RROT(e, 6) ^ SHA256.RROT(e, 11) ^ SHA256.RROT(e, 25);
var ch = (e & f) ^ ((~e) & g);
var temp1 = h + S1 + ch + k[i] + w[i];
var S0 = SHA256.RROT(a, 2) ^ SHA256.RROT(a, 13) ^ SHA256.RROT(a, 22);
var maj = (a & b) ^ (a & c) ^ (b & c);
uint temp2 = S0 + maj;
// Produce the final hash value (big-endian):
//digest := hash := h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
h = g;
g = f;
f = e;
e = (d + temp1) >> 0;
d = c;
c = b;
b = a;
a = (temp1 + temp2) >> 0;
}
var results = new BinaryList();
for (var i = 0; i < 8; i++)
results.AddUInt32(hash[i]);
// Add the compressed chunk to the current hash value:
hash[0] = (hash[0] + a) >> 0;
hash[1] = (hash[1] + b) >> 0;
hash[2] = (hash[2] + c) >> 0;
hash[3] = (hash[3] + d) >> 0;
hash[4] = (hash[4] + e) >> 0;
hash[5] = (hash[5] + f) >> 0;
hash[6] = (hash[6] + g) >> 0;
hash[7] = (hash[7] + h) >> 0;
return results.ToArray();
}
// Produce the final hash value (big-endian):
//digest := hash := h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
var results = new BinaryList();
for (var i = 0; i < 8; i++)
results.AddUInt32(hash[i]);
return results.ToArray();
}
}