Browse Source

use span instead of new byte[] in table cipher key generation

update build target
pull/2865/head
Student Main 4 years ago
parent
commit
c2bf74c554
4 changed files with 15 additions and 30 deletions
  1. +13
    -15
      shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs
  2. +1
    -1
      shadowsocks-csharp/shadowsocks-csharp.csproj
  3. +0
    -10
      shadowsocks-windows.sln
  4. +1
    -4
      test/CryptographyTest.cs

+ 13
- 15
shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

@@ -25,11 +26,13 @@ namespace Shadowsocks.Encryption.Stream
{
_encryptTable[i] = (byte)i;
}
Span<byte> t = _encryptTable;
// copy array 1024 times? excuse me?
for (int i = 1; i < 1024; i++)
{
_encryptTable = MergeSort(_encryptTable, a, i);
t = MergeSort(t, a, i);
}
_encryptTable = t.ToArray();
for (int i = 0; i < 256; i++)
{
_decryptTable[_encryptTable[i]] = (byte)i;
@@ -103,37 +106,32 @@ namespace Shadowsocks.Encryption.Stream
#region Table
private byte[] _encryptTable = new byte[256];
private byte[] _decryptTable = new byte[256];
private byte[] _tmp = new byte[256];

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long Compare(byte x, byte y, ulong a, int i)
{
return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i));
}

private byte[] MergeSort(byte[] array, ulong a, int j)
byte[] buf = new byte[1024];
private Span<byte> MergeSort(Span<byte> array, ulong a, int j)
{
if (array.Length == 1)
{
return array;
}
int middle = array.Length / 2;
byte[] left = new byte[middle];
for (int i = 0; i < middle; i++)
{
left[i] = array[i];
}
byte[] right = new byte[array.Length - middle];
for (int i = 0; i < array.Length - middle; i++)
{
right[i] = array[i + middle];
}
left = MergeSort(left, a, j);
right = MergeSort(right, a, j);

Span<byte> left = MergeSort(array.Slice(0, middle), a, j);;
Span<byte> right = MergeSort(array.Slice(middle), a, j);

int leftptr = 0;
int rightptr = 0;

// why a new array?
byte[] sorted = new byte[array.Length];
Span<byte> sorted = new byte[array.Length];// buf.AsSpan().Slice(0,array.Length); // // _tmp;
for (int k = 0; k < array.Length; k++)
{
if (rightptr == right.Length || ((leftptr < left.Length) && (Compare(left[leftptr], right[rightptr], a, j) <= 0)))


+ 1
- 1
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -12,7 +12,7 @@
<AssemblyName>Shadowsocks</AssemblyName>
<ApplicationIcon>shadowsocks.ico</ApplicationIcon>
<StartupObject>Shadowsocks.Program</StartupObject>
<Nullable>enable</Nullable>
<Nullable>disable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">


+ 0
- 10
shadowsocks-windows.sln View File

@@ -26,27 +26,17 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.ActiveCfg = Debug|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Build.0 = Debug|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|Any CPU.Build.0 = Release|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.ActiveCfg = Release|Any CPU
{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.Build.0 = Release|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.ActiveCfg = Debug|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.Build.0 = Debug|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.Build.0 = Release|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.ActiveCfg = Release|Any CPU
{45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE


+ 1
- 4
test/CryptographyTest.cs View File

@@ -155,10 +155,7 @@ namespace Shadowsocks.Test
[TestMethod]
public void TestNativeTableEncryption()
{
// Too slow, run once to save CPU
var enc = new StreamTableNativeEncryptor("table", "barfoo!");
var dec = new StreamTableNativeEncryptor("table", "barfoo!");
RunEncryptionRound(enc, dec);
TestEncryptionMethod(typeof(StreamTableNativeEncryptor), "table");
}
[TestMethod]
public void TestStreamAesBouncyCastleEncryption()


Loading…
Cancel
Save