Skip to content

Commit 319a557

Browse files
committed
byte[] => ReadOnlySpan<byte>
1 parent b92479d commit 319a557

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+148
-136
lines changed

CryptoBase.Abstractions/SymmetricCryptos/RC4CryptoBase.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace CryptoBase.Abstractions.SymmetricCryptos
24
{
35
public abstract class SnuffleCryptoBase : StreamCryptoBase
@@ -6,6 +8,6 @@ public abstract class SnuffleCryptoBase : StreamCryptoBase
68

79
public virtual int IvSize => 8;
810

9-
protected SnuffleCryptoBase(byte[] key, byte[] iv) { }
11+
protected SnuffleCryptoBase(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv) { }
1012
}
1113
}

CryptoBase.BouncyCastle/SymmetricCryptos/StreamCryptos/BcRC4Crypto.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
namespace CryptoBase.BouncyCastle.SymmetricCryptos.StreamCryptos
77
{
8-
public class BcRC4Crypto : RC4CryptoBase
8+
public class BcRC4Crypto : StreamCryptoBase
99
{
10+
public override string Name => @"RC4";
11+
1012
private readonly RC4Engine _rc4;
1113

12-
public BcRC4Crypto(byte[] key) : base(key)
14+
public BcRC4Crypto(byte[] key)
1315
{
1416
_rc4 = new RC4Engine();
1517
_rc4.Init(default, new KeyParameter(key));

CryptoBase/AEADCryptoCreate.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22
using CryptoBase.SymmetricCryptos.AEADCryptos;
33
using CryptoBase.SymmetricCryptos.AEADCryptos.GCM;
44
using CryptoBase.SymmetricCryptos.BlockCryptos.SM4;
5+
using System;
56
using System.Runtime.CompilerServices;
67

78
namespace CryptoBase
89
{
910
public static class AEADCryptoCreate
1011
{
1112
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12-
public static IAEADCrypto AesGcm(byte[] key)
13+
public static IAEADCrypto AesGcm(ReadOnlySpan<byte> key)
1314
{
1415
return new DefaultAesGcmCrypto(key);
1516
}
1617

1718
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18-
public static IAEADCrypto Sm4Gcm(byte[] key)
19+
public static IAEADCrypto Sm4Gcm(ReadOnlySpan<byte> key)
1920
{
2021
return new GcmCryptoMode(new SM4Crypto(key));
2122
}
2223

2324
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24-
public static IAEADCrypto ChaCha20Poly1305(byte[] key)
25+
public static IAEADCrypto ChaCha20Poly1305(ReadOnlySpan<byte> key)
2526
{
2627
return new ChaCha20Poly1305Crypto(key);
2728
}
2829

2930
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
public static IAEADCrypto XChaCha20Poly1305(byte[] key)
31+
public static IAEADCrypto XChaCha20Poly1305(ReadOnlySpan<byte> key)
3132
{
3233
return new XChaCha20Poly1305Crypto(key);
3334
}

CryptoBase/AESUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static AESUtils()
2626
}
2727

2828
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
public static AESCrypto CreateECB(byte[] key)
29+
public static AESCrypto CreateECB(ReadOnlySpan<byte> key)
3030
{
3131
if (System.Runtime.Intrinsics.X86.Aes.IsSupported && Sse2.IsSupported)
3232
{
@@ -43,7 +43,7 @@ public static AESCrypto CreateECB(byte[] key)
4343
}
4444

4545
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46-
public static IBlockCrypto CreateCBC(byte[] key, byte[] iv)
46+
public static IBlockCrypto CreateCBC(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
4747
{
4848
if (System.Runtime.Intrinsics.X86.Aes.IsSupported && Sse2.IsSupported)
4949
{

CryptoBase/GHashUtils.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CryptoBase.Abstractions;
22
using CryptoBase.Macs.GHash;
3+
using System;
34
using System.Runtime.CompilerServices;
45
using System.Runtime.Intrinsics.X86;
56

@@ -8,7 +9,7 @@ namespace CryptoBase
89
public static class GHashUtils
910
{
1011
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11-
public static IMac Create(byte[] key)
12+
public static IMac Create(ReadOnlySpan<byte> key)
1213
{
1314
if (Sse2.IsSupported && Ssse3.IsSupported && Pclmulqdq.IsSupported)
1415
{

CryptoBase/Macs/GHash/GHashSF.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ public class GHashSF : IMac
2727
private readonly ulong[] _hl;
2828
private readonly byte[] _buffer;
2929

30-
private readonly ReadOnlyMemory<byte> _key;
30+
private readonly ulong Initvh;
31+
private readonly ulong Initvl;
3132

32-
public GHashSF(byte[] key)
33+
public GHashSF(ReadOnlySpan<byte> key)
3334
{
3435
if (key.Length < KeySize)
3536
{
3637
throw new ArgumentException(@"Key length must be 16 bytes", nameof(key));
3738
}
3839

39-
_key = key;
40+
Initvh = BinaryPrimitives.ReadUInt64BigEndian(key);
41+
Initvl = BinaryPrimitives.ReadUInt64BigEndian(key.Slice(8));
4042

4143
_hl = ArrayPool<ulong>.Shared.Rent(BlockSize);
4244
_hh = ArrayPool<ulong>.Shared.Rent(BlockSize);
@@ -115,8 +117,8 @@ public void Reset()
115117
{
116118
CryptographicOperations.ZeroMemory(_buffer.AsSpan(0, BlockSize));
117119

118-
var vh = BinaryPrimitives.ReadUInt64BigEndian(_key.Span);
119-
var vl = BinaryPrimitives.ReadUInt64BigEndian(_key.Span.Slice(8));
120+
var vh = Initvh;
121+
var vl = Initvl;
120122

121123
_hl[8] = vl;
122124
_hh[8] = vh;

CryptoBase/Macs/GHash/GHashX86.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GHashX86 : IMac
1717
private readonly Vector128<byte> _key;
1818
private Vector128<byte> _buffer;
1919

20-
public unsafe GHashX86(byte[] key)
20+
public unsafe GHashX86(ReadOnlySpan<byte> key)
2121
{
2222
if (key.Length < KeySize)
2323
{

CryptoBase/StreamCryptoCreate.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,50 @@
88
using CryptoBase.SymmetricCryptos.StreamCryptos.Salsa20;
99
using CryptoBase.SymmetricCryptos.StreamCryptos.XChaCha20;
1010
using CryptoBase.SymmetricCryptos.StreamCryptos.XSalsa20;
11+
using System;
1112
using System.Runtime.CompilerServices;
1213
using System.Runtime.Intrinsics.X86;
1314

1415
namespace CryptoBase
1516
{
1617
public static class StreamCryptoCreate
1718
{
18-
private static readonly byte[] EmptyIv12 = new byte[12];
19-
private static readonly byte[] EmptyIv24 = new byte[24];
19+
private static ReadOnlySpan<byte> EmptyIv12 => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
20+
21+
private static ReadOnlySpan<byte> EmptyIv24 => new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2022

2123
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public static IStreamCrypto AesCtr(byte[] key, byte[] iv)
24+
public static IStreamCrypto AesCtr(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
2325
{
2426
return new CTRStreamMode(AESUtils.CreateECB(key), iv);
2527
}
2628

2729
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28-
public static IStreamCrypto Sm4Ctr(byte[] key, byte[] iv)
30+
public static IStreamCrypto Sm4Ctr(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
2931
{
3032
return new CTRStreamMode(new SM4Crypto(key), iv);
3133
}
3234

3335
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34-
public static IStreamCrypto AesCfb(bool isEncrypt, byte[] key, byte[] iv)
36+
public static IStreamCrypto AesCfb(bool isEncrypt, ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
3537
{
3638
return new CFB128StreamMode(isEncrypt, AESUtils.CreateECB(key), iv);
3739
}
3840

3941
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40-
public static IStreamCrypto Sm4Cfb(bool isEncrypt, byte[] key, byte[] iv)
42+
public static IStreamCrypto Sm4Cfb(bool isEncrypt, ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
4143
{
4244
return new CFB128StreamMode(isEncrypt, new SM4Crypto(key), iv);
4345
}
4446

4547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46-
public static IStreamCrypto Rc4(byte[] key)
48+
public static IStreamCrypto Rc4(ReadOnlySpan<byte> key)
4749
{
4850
return new RC4Crypto(key);
4951
}
5052

5153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52-
public static ChaCha20OriginalCrypto ChaCha20Original(byte[] key, byte[] iv)
54+
public static ChaCha20OriginalCrypto ChaCha20Original(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
5355
{
5456
if (Sse2.IsSupported)
5557
{
@@ -60,7 +62,7 @@ public static ChaCha20OriginalCrypto ChaCha20Original(byte[] key, byte[] iv)
6062
}
6163

6264
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
public static ChaCha20Crypto ChaCha20(byte[] key, byte[] iv)
65+
public static ChaCha20Crypto ChaCha20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
6466
{
6567
if (Sse2.IsSupported)
6668
{
@@ -71,13 +73,13 @@ public static ChaCha20Crypto ChaCha20(byte[] key, byte[] iv)
7173
}
7274

7375
[MethodImpl(MethodImplOptions.AggressiveInlining)]
74-
public static ChaCha20Crypto ChaCha20(byte[] key)
76+
public static ChaCha20Crypto ChaCha20(ReadOnlySpan<byte> key)
7577
{
7678
return ChaCha20(key, EmptyIv12);
7779
}
7880

7981
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80-
public static XChaCha20Crypto XChaCha20(byte[] key, byte[] iv)
82+
public static XChaCha20Crypto XChaCha20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
8183
{
8284
if (Sse2.IsSupported)
8385
{
@@ -88,13 +90,13 @@ public static XChaCha20Crypto XChaCha20(byte[] key, byte[] iv)
8890
}
8991

9092
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91-
public static XChaCha20Crypto XChaCha20(byte[] key)
93+
public static XChaCha20Crypto XChaCha20(ReadOnlySpan<byte> key)
9294
{
9395
return XChaCha20(key, EmptyIv24);
9496
}
9597

9698
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97-
public static Salsa20Crypto Salsa20(byte[] key, byte[] iv)
99+
public static Salsa20Crypto Salsa20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
98100
{
99101
if (Sse2.IsSupported)
100102
{
@@ -105,7 +107,7 @@ public static Salsa20Crypto Salsa20(byte[] key, byte[] iv)
105107
}
106108

107109
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108-
public static Salsa20Crypto XSalsa20(byte[] key, byte[] iv)
110+
public static Salsa20Crypto XSalsa20(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
109111
{
110112
if (Sse2.IsSupported)
111113
{

CryptoBase/SymmetricCryptos/AEADCryptos/ChaCha20Poly1305Crypto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class ChaCha20Poly1305Crypto : IAEADCrypto
2525

2626
private readonly byte[] _buffer;
2727

28-
public ChaCha20Poly1305Crypto(byte[] key)
28+
public ChaCha20Poly1305Crypto(ReadOnlySpan<byte> key)
2929
{
3030
if (key.Length < KeySize)
3131
{

0 commit comments

Comments
 (0)