Files
LightlessClient/LightlessSync/Utils/Crypto.cs
defnotken 835a0a637d
All checks were successful
Tag and Release Lightless / tag-and-release (push) Successful in 2m27s
2.0.0 (#92)
2.0.0 Changes:

- Reworked shell finder UI with compact or list view with profile tags showing with the listing, allowing moderators to broadcast the syncshell as well to have it be used more.
- Reworked user list in syncshell admin screen to have filter visible and moved away from table to its own thing, allowing to copy uid/note/alias when clicking on the name.
- Reworked download bars and download box to make it look more modern, removed the jitter around, so it shouldn't vibrate around much.
- Chat has been added to the top menu, working in Zone or in Syncshells to be used there.
- Paired system has been revamped to make pausing and unpausing faster, and loading people should be faster as well.
- Moved to the internal object table to have faster load times for users; people should load in faster
- Compactor is running on a multi-threaded level instead of single-threaded; this should increase the speed of compacting files
- Nameplate Service has been reworked so it wouldn't use the nameplate handler anymore.
- Files can be resized when downloading to reduce load on users if they aren't compressed. (can be toggled to resize all).
- Penumbra Collections are now only made when people are visible, reducing the load on boot-up when having many syncshells in your list.
- Lightfinder plates have been moved away from using Nameplates, but will use an overlay.
- Main UI has been changed a bit with a gradient, and on hover will glow up now.
- Reworked Profile UI for Syncshell and Users to be more user-facing with more customizable items.
- Reworked Settings UI to look more modern.
- Performance should be better due to new systems that would dispose of the collections and better caching of items.

Co-authored-by: defnotken <itsdefnotken@gmail.com>
Co-authored-by: azyges <aaaaaa@aaa.aaa>
Co-authored-by: choco <choco@patat.nl>
Co-authored-by: cake <admin@cakeandbanana.nl>
Co-authored-by: Minmoose <KennethBohr@outlook.com>
Reviewed-on: #92
2025-12-21 17:19:34 +00:00

241 lines
8.8 KiB
C#

using Blake3;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace LightlessSync.Utils;
public static class Crypto
{
//This buffersize seems to be the best sweetpoint for Linux and Windows
private const int _bufferSize = 65536;
#pragma warning disable SYSLIB0021 // Type or member is obsolete
private static readonly ConcurrentDictionary<(string, ushort), string> _hashListPlayersSHA256 = new();
private static readonly ConcurrentDictionary<string, string> _hashListSHA256 = new(StringComparer.Ordinal);
private static readonly SHA256CryptoServiceProvider _sha256CryptoProvider = new();
// BLAKE3 hash caches
private static readonly Dictionary<(string, ushort), string> _hashListPlayersBlake3 = [];
private static readonly Dictionary<string, string> _hashListBlake3 = new(StringComparer.Ordinal);
/// <summary>
/// Supports Blake3 or SHA1 for file transfers, no SHA256 supported on it
/// </summary>
public enum HashAlgo
{
Blake3,
Sha1
}
/// <summary>
/// Detects which algo is being used for the file
/// </summary>
/// <param name="hashHex">Hashed string</param>
/// <returns>HashAlgo</returns>
public static HashAlgo DetectAlgo(string hashHex)
{
if (hashHex.Length == 40)
return HashAlgo.Sha1;
return HashAlgo.Blake3;
}
#region File Hashing
/// <summary>
/// Compute file hash with given algorithm, supports BLAKE3 and Sha1 for file hashing
/// </summary>
/// <param name="filePath">Filepath for the hashing</param>
/// <param name="algo">BLAKE3 or Sha1</param>
/// <returns>Hashed file hash</returns>
/// <exception cref="ArgumentOutOfRangeException">Not a valid HashAlgo or Filepath</exception>
public static string ComputeFileHash(string filePath, HashAlgo algo)
{
return algo switch
{
HashAlgo.Blake3 => ComputeFileHashBlake3(filePath),
HashAlgo.Sha1 => ComputeFileHashSha1(filePath),
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, null)
};
}
/// <summary>
/// Compute file hash asynchronously with given algorithm, supports BLAKE3 and SHA1 for file hashing
/// </summary>
/// <param name="filePath">Filepath for the hashing</param>
/// <param name="algo">BLAKE3 or Sha1</param>
/// <returns>Hashed file hash</returns>
/// <exception cref="ArgumentOutOfRangeException">Not a valid HashAlgo or Filepath</exception>
public static async Task<string> ComputeFileHashAsync(string filePath, HashAlgo algo, CancellationToken cancellationToken = default)
{
return algo switch
{
HashAlgo.Blake3 => await ComputeFileHashBlake3Async(filePath, cancellationToken).ConfigureAwait(false),
HashAlgo.Sha1 => await ComputeFileHashSha1Async(filePath, cancellationToken).ConfigureAwait(false),
_ => throw new ArgumentOutOfRangeException(nameof(algo), algo, message: null)
};
}
/// <summary>
/// Computes an file hash with SHA1
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string</returns>
private static string ComputeFileHashSha1(string filePath)
{
using var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var sha1 = SHA1.Create();
var hash = sha1.ComputeHash(stream);
return Convert.ToHexString(hash);
}
/// <summary>
/// Computes an file hash with SHA1 asynchronously
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Hashed file in hex string hashed in SHA1</returns>
private static async Task<string> ComputeFileHashSha1Async(string filePath, CancellationToken cancellationToken)
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: _bufferSize, options: FileOptions.Asynchronous);
await using (stream.ConfigureAwait(false))
{
using var sha1 = SHA1.Create();
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false)) > 0)
{
sha1.TransformBlock(buffer, 0, bytesRead, outputBuffer: null, 0);
}
sha1.TransformFinalBlock([], 0, 0);
return Convert.ToHexString(sha1.Hash!);
}
}
/// <summary>
/// Computes an file hash with Blake3
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string hashed in Blake3</returns>
private static string ComputeFileHashBlake3(string filePath)
{
using var stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var hasher = Hasher.New();
var buffer = new byte[_bufferSize];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
hasher.Update(buffer.AsSpan(0, bytesRead));
}
var hash = hasher.Finalize();
return hash.ToString();
}
/// <summary>
/// Computes an file hash with Blake3 asynchronously
/// </summary>
/// <param name="filePath">Filepath that has to be computed</param>
/// <returns>Hashed file in hex string hashed in Blake3</returns>
private static async Task<string> ComputeFileHashBlake3Async(string filePath, CancellationToken cancellationToken)
{
var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, bufferSize: _bufferSize, options: FileOptions.Asynchronous);
await using (stream.ConfigureAwait(false))
{
using var hasher = Hasher.New();
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length), cancellationToken).ConfigureAwait(false)) > 0)
{
hasher.Update(buffer.AsSpan(0, bytesRead));
}
var hash = hasher.Finalize();
return hash.ToString();
}
}
#endregion
#region String hashing
public static string GetBlake3Hash(this (string, ushort) playerToHash)
{
if (_hashListPlayersBlake3.TryGetValue(playerToHash, out var hash))
return hash;
var toHash = playerToHash.Item1 + playerToHash.Item2.ToString();
hash = ComputeBlake3Hex(toHash);
_hashListPlayersBlake3[playerToHash] = hash;
return hash;
}
/// <summary>
/// Computes or gets an Blake3 hash(ed) string.
/// </summary>
/// <param name="stringToHash">String that needs to be hashsed</param>
/// <returns>Hashed string</returns>
public static string GetBlake3Hash(this string stringToHash)
{
return GetOrComputeBlake3(stringToHash);
}
private static string GetOrComputeBlake3(string stringToCompute)
{
if (_hashListBlake3.TryGetValue(stringToCompute, out var hash))
return hash;
hash = ComputeBlake3Hex(stringToCompute);
_hashListBlake3[stringToCompute] = hash;
return hash;
}
private static string ComputeBlake3Hex(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
var hash = Hasher.Hash(bytes);
return Convert.ToHexString(hash.AsSpan());
}
public static string GetHash256(this (string, ushort) playerToHash)
{
if (_hashListPlayersSHA256.TryGetValue(playerToHash, out var hash))
return hash;
return _hashListPlayersSHA256[playerToHash] =
Convert.ToHexString(_sha256CryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(playerToHash.Item1 + playerToHash.Item2.ToString())));
}
/// <summary>
/// Computes or gets an SHA256 hash(ed) string.
/// </summary>
/// <param name="stringToHash">String that needs to be hashsed</param>
/// <returns>Hashed string</returns>
public static string GetHash256(this string stringToHash)
{
return _hashListSHA256.GetOrAdd(stringToHash, ComputeHashSHA256);
}
private static string ComputeHashSHA256(string stringToCompute)
{
if (_hashListSHA256.TryGetValue(stringToCompute, out var hash))
return hash;
return _hashListSHA256[stringToCompute] =
Convert.ToHexString(_sha256CryptoProvider.ComputeHash(Encoding.UTF8.GetBytes(stringToCompute)));
}
#endregion
#pragma warning restore SYSLIB0021 // Type or member is obsolete
}