Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c67c709
Initial plan
Copilot Oct 11, 2025
7f3d214
Add Windows-specific BCrypt implementation for Sha1ForNonSecretPurposes
Copilot Oct 11, 2025
22b79f2
Use MemoryMarshal.GetReference for safer pointer handling
Copilot Oct 11, 2025
51cac6c
Refactor: Split Sha1ForNonSecretPurposes into platform-specific files
Copilot Oct 11, 2025
6dafca3
Update src/libraries/System.Security.Cryptography/src/System.Security…
jkotas Oct 11, 2025
abb74e0
Address review feedback: Remove Unix file from csproj and reorder Win…
Copilot Oct 11, 2025
cf4d613
Delete original Sha1ForNonSecretPurposes.cs file
Copilot Oct 11, 2025
c2cc664
Use streaming BCrypt hash APIs for better performance
Copilot Oct 11, 2025
06608fe
Add SafeBCryptAlgorithmHandle to fix build error
Copilot Oct 12, 2025
0438e94
Simplify to one-shot HashData API per feedback
Copilot Oct 12, 2025
fb515a0
Add Unix Sha1 file for Browser build
Copilot Oct 12, 2025
86075cd
Optimize GenerateGuidFromName to reduce allocations
Copilot Oct 12, 2025
0efb9dd
Apply suggestions from code review
jkotas Oct 12, 2025
793033b
Update src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Windo…
jkotas Oct 12, 2025
9ee1744
Fix bug, optimize one-shot managed hash
jkotas Oct 12, 2025
46dd271
Update src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs
jkotas Oct 12, 2025
102238a
Update src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Windo…
jkotas Oct 12, 2025
5aa39a4
Update src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Windo…
jkotas Oct 12, 2025
b60ffd4
Update src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Windo…
jkotas Oct 12, 2025
4a07041
Add BCryptHash to direct pinvoke list
jkotas Oct 13, 2025
2936b48
Update src/coreclr/nativeaot/BuildIntegration/WindowsAPIs.txt
jkotas Oct 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Numerics;

namespace System
{
/// <summary>
/// Implements the SHA1 hashing algorithm. Note that
/// implementation is for hashing public information. Do not
/// use code to hash private data, as implementation does
/// not take any steps to avoid information disclosure.
/// </summary>
internal struct Sha1ForNonSecretPurposes

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / dotnet-linker-tests (Build browser-wasm linux release Runtime_Release)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'

Check failure on line 14 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_Smoke_AOT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L14

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(14,21): error CS0101: (NETCORE_ENGINEERING_TELEMETRY=Build) The namespace 'System' already contains a definition for 'Sha1ForNonSecretPurposes'
{
private long _length; // Total message length in bits
private uint[] _w; // Workspace
private int _pos; // Length of current chunk in bytes

/// <summary>
/// Call Start() to initialize the hash object.
/// </summary>
public void Start()

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / dotnet-linker-tests (Build browser-wasm linux release Runtime_Release)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types

Check failure on line 23 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_Smoke_AOT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L23

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(23,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Start' with the same parameter types
{
_w ??= new uint[85];

_length = 0;
_pos = 0;
_w[80] = 0x67452301;
_w[81] = 0xEFCDAB89;
_w[82] = 0x98BADCFE;
_w[83] = 0x10325476;
_w[84] = 0xC3D2E1F0;
}

/// <summary>
/// Adds an input byte to the hash.
/// </summary>
/// <param name="input">Data to include in the hash.</param>
public void Append(byte input)

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / dotnet-linker-tests (Build browser-wasm linux release Runtime_Release)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 40 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_Smoke_AOT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L40

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(40,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types
{
int idx = _pos >> 2;
_w[idx] = (_w[idx] << 8) | input;
if (64 == ++_pos)
{
Drain();
}
}

/// <summary>
/// Adds input bytes to the hash.
/// </summary>
/// <param name="input">
/// Data to include in the hash. Must not be null.
/// </param>
public void Append(ReadOnlySpan<byte> input)

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / dotnet-linker-tests (Build browser-wasm linux release Runtime_Release)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types

Check failure on line 56 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_Smoke_AOT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L56

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(56,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Append' with the same parameter types
{
foreach (byte b in input)
{
Append(b);
}
}

/// <summary>
/// Retrieves the hash value.
/// Note that after calling function, the hash object should
/// be considered uninitialized. Subsequent calls to Append or
/// Finish will produce useless results. Call Start() to
/// reinitialize.
/// </summary>
/// <param name="output">
/// Buffer to receive the hash value. Must not be null.
/// Up to 20 bytes of hash will be written to the output buffer.
/// If the buffer is smaller than 20 bytes, the remaining hash
/// bytes will be lost. If the buffer is larger than 20 bytes, the
/// rest of the buffer is left unmodified.
/// </param>
public void Finish(Span<byte> output)

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build linux-x64 debug Libraries_WithPackages)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime-dev-innerloop (Build Source-Build (Linux_x64))

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / dotnet-linker-tests (Build browser-wasm linux release Runtime_Release)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_EAT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types

Check failure on line 78 in src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs

View check run for this annotation

Azure Pipelines / runtime (Build browser-wasm linux Release LibraryTests_Smoke_AOT)

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs#L78

src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Unix.cs(78,21): error CS0111: (NETCORE_ENGINEERING_TELEMETRY=Build) Type 'Sha1ForNonSecretPurposes' already defines a member called 'Finish' with the same parameter types
{
long l = _length + 8 * _pos;
Append(0x80);
while (_pos != 56)
{
Append(0x00);
}

Append((byte)(l >> 56));
Append((byte)(l >> 48));
Append((byte)(l >> 40));
Append((byte)(l >> 32));
Append((byte)(l >> 24));
Append((byte)(l >> 16));
Append((byte)(l >> 8));
Append((byte)l);

int end = output.Length < 20 ? output.Length : 20;
for (int i = 0; i != end; i++)
{
uint temp = _w[80 + i / 4];
output[i] = (byte)(temp >> 24);
_w[80 + i / 4] = temp << 8;
}
}

/// <summary>
/// Called when pos reaches 64.
/// </summary>
private void Drain()
{
for (int i = 16; i != 80; i++)
{
_w[i] = BitOperations.RotateLeft(_w[i - 3] ^ _w[i - 8] ^ _w[i - 14] ^ _w[i - 16], 1);
}

uint a = _w[80];
uint b = _w[81];
uint c = _w[82];
uint d = _w[83];
uint e = _w[84];

for (int i = 0; i != 20; i++)
{
const uint k = 0x5A827999;
uint f = (b & c) | ((~b) & d);
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 20; i != 40; i++)
{
uint f = b ^ c ^ d;
const uint k = 0x6ED9EBA1;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 40; i != 60; i++)
{
uint f = (b & c) | (b & d) | (c & d);
const uint k = 0x8F1BBCDC;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

for (int i = 60; i != 80; i++)
{
uint f = b ^ c ^ d;
const uint k = 0xCA62C1D6;
uint temp = BitOperations.RotateLeft(a, 5) + f + e + k + _w[i]; e = d; d = c; c = BitOperations.RotateLeft(b, 30); b = a; a = temp;
}

_w[80] += a;
_w[81] += b;
_w[82] += c;
_w[83] += d;
_w[84] += e;

_length += 512; // 64 bytes == 512 bits
_pos = 0;
}
}
}
123 changes: 123 additions & 0 deletions src/libraries/Common/src/System/Sha1ForNonSecretPurposes.Windows.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace System
{
/// <summary>
/// Implements the SHA1 hashing algorithm. Note that
/// implementation is for hashing public information. Do not
/// use code to hash private data, as implementation does
/// not take any steps to avoid information disclosure.
/// </summary>
internal struct Sha1ForNonSecretPurposes
{
private byte[]? _buffer;
private int _bufferPos;

/// <summary>
/// Call Start() to initialize the hash object.
/// </summary>
public void Start()
{
_buffer = null;
_bufferPos = 0;
}

/// <summary>
/// Adds an input byte to the hash.
/// </summary>
/// <param name="input">Data to include in the hash.</param>
public void Append(byte input)
{
_buffer ??= new byte[256];

if (_bufferPos == _buffer.Length)
{
byte[] newBuffer = new byte[_buffer.Length * 2];
_buffer.CopyTo(newBuffer, 0);
_buffer = newBuffer;
}

_buffer[_bufferPos++] = input;
}

/// <summary>
/// Adds input bytes to the hash.
/// </summary>
/// <param name="input">
/// Data to include in the hash. Must not be null.
/// </param>
public void Append(ReadOnlySpan<byte> input)
{
if (input.IsEmpty)
{
return;
}

_buffer ??= new byte[256];

int requiredSize = _bufferPos + input.Length;
if (requiredSize > _buffer.Length)
{
int newSize = _buffer.Length;
while (newSize < requiredSize)
{
newSize *= 2;
}
byte[] newBuffer = new byte[newSize];
_buffer.AsSpan(0, _bufferPos).CopyTo(newBuffer);
_buffer = newBuffer;
}

input.CopyTo(_buffer.AsSpan(_bufferPos));
_bufferPos += input.Length;
}

/// <summary>
/// Retrieves the hash value.
/// Note that after calling function, the hash object should
/// be considered uninitialized. Subsequent calls to Append or
/// Finish will produce useless results. Call Start() to
/// reinitialize.
/// </summary>
/// <param name="output">
/// Buffer to receive the hash value. Must not be null.
/// Up to 20 bytes of hash will be written to the output buffer.
/// If the buffer is smaller than 20 bytes, the remaining hash
/// bytes will be lost. If the buffer is larger than 20 bytes, the
/// rest of the buffer is left unmodified.
/// </param>
public void Finish(Span<byte> output)
{
ReadOnlySpan<byte> source = _buffer is null ? ReadOnlySpan<byte>.Empty : new ReadOnlySpan<byte>(_buffer, 0, _bufferPos);

unsafe
{
fixed (byte* pSrc = &MemoryMarshal.GetReference(source))
fixed (byte* pDest = &MemoryMarshal.GetReference(output))
{
Interop.BCrypt.NTSTATUS ntStatus = Interop.BCrypt.BCryptHash(
(uint)Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_SHA1_ALG_HANDLE,
null,
0,
pSrc,
source.Length,
pDest,
Math.Min(output.Length, 20));

if (ntStatus != Interop.BCrypt.NTSTATUS.STATUS_SUCCESS)
{
int hr = unchecked((int)ntStatus) | 0x01000000;
throw new CryptographicException(hr);
}
}
}

_buffer = null;
_bufferPos = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1486,9 +1486,6 @@
<Compile Include="$(CommonPath)System\Obsoletions.cs">
<Link>Common\System\Obsoletions.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs">
<Link>Common\System\Sha1ForNonSecretPurposes.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\SR.cs">
<Link>Common\System\SR.cs</Link>
</Compile>
Expand Down Expand Up @@ -1750,6 +1747,15 @@
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.NTSTATUS.cs">
<Link>Common\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.BCryptHash.cs">
<Link>Common\Interop\Windows\BCrypt\Interop.BCryptHash.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\BCrypt\Interop.BCryptAlgPseudoHandle.cs">
<Link>Common\Interop\Windows\BCrypt\Interop.BCryptAlgPseudoHandle.cs</Link>
</Compile>
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.Windows.cs">
<Link>Common\System\Sha1ForNonSecretPurposes.Windows.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Windows\Crypt32\Interop.CryptProtectMemory.cs">
<Link>Common\Interop\Windows\Crypt32\Interop.CryptProtectMemory.cs</Link>
</Compile>
Expand Down Expand Up @@ -2349,6 +2355,9 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Threading\EventWaitHandle.Windows.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true' or '$(TargetsWasi)' == 'true'">
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.Unix.cs">
<Link>Common\System\Sha1ForNonSecretPurposes.Unix.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs">
<Link>Common\Interop\Unix\Interop.Errors.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,10 @@
Link="Common\Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
<Compile Include="$(CommonPath)Interop\Browser\Interop.Libraries.cs"
Link="Common\Interop\Browser\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs"
Link="Common\System\Sha1ForNonSecretPurposes.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.Windows.cs"
Link="Common\System\Sha1ForNonSecretPurposes.Windows.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.Unix.cs"
Link="Common\System\Sha1ForNonSecretPurposes.Unix.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\CompositeMLDsaImplementation.NotSupported.cs"
Link="Common\System\Security\Cryptography\CompositeMLDsaImplementation.NotSupported.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\SP800108HmacCounterKdfImplementationManaged.cs"
Expand Down
Loading