This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: avoid updating when it's not needed (reduce resource consump…
…tion) (#74) * feature: avoid updating when it's not needed * bump chart to 0.14.0
- Loading branch information
Showing
9 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Bitwarden.SecretOperator.Helpers; | ||
|
||
public static class DictionaryHelper | ||
{ | ||
|
||
public static string ComputeHash(this Dictionary<string, string> dict) | ||
{ | ||
// Sorting dictionary by keys to ensure consistent ordering | ||
IEnumerable<string> combinedPairs = dict | ||
.OrderBy(kvp => kvp.Key) | ||
.Select(kvp => $"{kvp.Key}={kvp.Value}"); | ||
|
||
string concatenatedString = string.Join("|", combinedPairs); | ||
|
||
byte[] inputBytes = Encoding.UTF8.GetBytes(concatenatedString); | ||
byte[] hashedBytes = SHA256.HashData(inputBytes); | ||
|
||
return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); | ||
} | ||
|
||
public static string ComputeHash(this Dictionary<string, byte[]> dict) | ||
{ | ||
// Sorting dictionary by keys to ensure consistent ordering | ||
List<KeyValuePair<string, byte[]>> sortedDict = dict.OrderBy(kvp => kvp.Key).ToList(); | ||
|
||
using var stream = new MemoryStream(); | ||
foreach (KeyValuePair<string, byte[]> pair in sortedDict) | ||
{ | ||
byte[] keyBytes = Encoding.UTF8.GetBytes(pair.Key); | ||
stream.Write(keyBytes, 0, keyBytes.Length); | ||
stream.WriteByte((byte)'|'); | ||
stream.Write(pair.Value, 0, pair.Value.Length); | ||
} | ||
|
||
return BitConverter.ToString(SHA256.HashData(stream.ToArray())).Replace("-", "").ToLower(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Bitwarden.SecretOperator.Tests/Bitwarden.SecretOperator.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> | ||
<PackageReference Include="NFluent" Version="3.0.1.352" /> | ||
<PackageReference Include="xunit" Version="2.5.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Bitwarden.SecretOperator\Bitwarden.SecretOperator.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
54 changes: 54 additions & 0 deletions
54
tests/Bitwarden.SecretOperator.Tests/DictionaryHashComputation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Text; | ||
using Bitwarden.SecretOperator.Helpers; | ||
using NFluent; | ||
|
||
namespace Bitwarden.SecretOperator.Tests; | ||
|
||
public class DictionaryHashComputation | ||
{ | ||
[Fact] | ||
public void ComputeHashDictionaryStringString() | ||
{ | ||
var dico1 = new Dictionary<string, string>() | ||
{ | ||
{"test1", "test2"}, | ||
{"test2", "test3"} | ||
}; | ||
var dico2 = new Dictionary<string, string>() | ||
{ | ||
{"test1", "test3"}, | ||
{"test2", "test2"} | ||
}; | ||
|
||
string hash1 = dico1.ComputeHash(); | ||
string hash1Bis = dico1.ComputeHash(); | ||
Check.That(hash1).IsEqualTo(hash1Bis); | ||
|
||
string hash2 = dico2.ComputeHash(); | ||
|
||
Check.That(hash1).IsNotEqualTo(hash2); | ||
} | ||
|
||
[Fact] | ||
public void ComputeHashDictionaryStringByteArray() | ||
{ | ||
var dico1 = new Dictionary<string, byte[]>() | ||
{ | ||
{"test1", "test2"u8.ToArray()}, | ||
{"test2", "test3"u8.ToArray()} | ||
}; | ||
var dico2 = new Dictionary<string, byte[]>() | ||
{ | ||
{"test1", "test3"u8.ToArray()}, | ||
{"test2", "test2"u8.ToArray()} | ||
}; | ||
|
||
string hash1 = dico1.ComputeHash(); | ||
string hash1Bis = dico1.ComputeHash(); | ||
Check.That(hash1).IsEqualTo(hash1Bis); | ||
|
||
string hash2 = dico2.ComputeHash(); | ||
|
||
Check.That(hash1).IsNotEqualTo(hash2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |