Skip to content

Commit

Permalink
Add TryGet* methods to ObfuscationDictionary, enable NuGet publish (#62)
Browse files Browse the repository at this point in the history
* Minor refactor TestApp, update file extensions

* Add TryGet* methods to ObfuscationDictionary

* Make UnobfuscatedValues property non-nullable

* Enable nuget push in release-obfuscator-common.yml
  • Loading branch information
albertospelta authored Sep 16, 2024
1 parent 50e8d04 commit 668d33a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/release-obfuscator-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
- name: dotnet pack
run: dotnet pack -c Release 'src/${{ env.PROJECT }}/${{ env.PROJECT }}.csproj'
- name: nuget push
run: echo "::warning::(TOFIX) enable nuget push."
run: dotnet nuget push '${{ env.NUPKG_PATH }}' -k '${{ secrets.NUGET_API_KEY }}' -s https://api.nuget.org/v3/index.json
env:
NUPKG_PATH: 'src/artifacts/package/release/${{ env.PROJECT }}.${{ steps.nbgv.outputs.NuGetPackageVersion }}.nupkg'
- name: create release
run: |
PRERELEASE_FLAG=$([[ "${{ steps.nbgv.outputs.PrereleaseVersion }}" != "" ]] && echo "--prerelease" || echo "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsPackable>true</IsPackable>
</PropertyGroup>

<PropertyGroup>
<Title>Vpax Obfuscator Common</Title>
<Description>A shared package used by VertiPaq Analyzer Obfuscator. Do not install this package manually, it will be added as a prerequisite by other packages that require it.</Description>
<Description>This package is a dependency for the VertiPaq Analyzer Obfuscator NuGet package and is automatically included as a prerequisite. It should not be installed manually.</Description>
</PropertyGroup>

<ItemGroup>
Expand Down
32 changes: 29 additions & 3 deletions src/Dax.Vpax.Obfuscator.Common/ObfuscationDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Newtonsoft.Json;

namespace Dax.Vpax.Obfuscator.Common;
Expand Down Expand Up @@ -26,16 +27,17 @@ public ObfuscationDictionary(string id, string version, IEnumerable<ObfuscationT
Id = id;
Version = version;
Texts = texts.OrderBy((t) => t.Value).ToArray();
UnobfuscatedValues = Array.Empty<string>();

// Create dictionaries to allow for fast lookups. This also ensures uniqueness of the keys by throwing if there are duplicates.
// Create dictionaries to enable fast lookups and ensure key uniqueness. An error will be thrown if duplicate keys are detected.
_values = Texts.ToDictionary((text) => text.Value, StringComparer.OrdinalIgnoreCase);
_obfuscated = Texts.ToDictionary((text) => text.Obfuscated, StringComparer.OrdinalIgnoreCase);
}

public string Id { get; }
public string Version { get; }
public IReadOnlyList<ObfuscationText> Texts { get; }
public IReadOnlyList<string>? UnobfuscatedValues { get; }
public IReadOnlyList<string> UnobfuscatedValues { get; }

public string GetValue(string obfuscated)
{
Expand All @@ -45,6 +47,18 @@ public string GetValue(string obfuscated)
throw new KeyNotFoundException($"The obfuscated value was not found in the dictionary [{obfuscated}].");
}

public bool TryGetValue(string obfuscated, [NotNullWhen(true)] out string? value)
{
if (_obfuscated.TryGetValue(obfuscated, out var text))
{
value = text.Value;
return true;
}

value = null;
return false;
}

public string GetObfuscated(string value)
{
if (_values.TryGetValue(value, out var text))
Expand All @@ -53,6 +67,18 @@ public string GetObfuscated(string value)
throw new KeyNotFoundException($"The value was not found in the dictionary [{value}].");
}

public bool TryGetObfuscated(string value, [NotNullWhen(true)] out string? obfuscated)
{
if (_values.TryGetValue(value, out var text))
{
obfuscated = text.Obfuscated;
return true;
}

obfuscated = null;
return false;
}

public void WriteTo(string path, bool overwrite = false, bool indented = true)
{
var mode = overwrite ? FileMode.Create : FileMode.CreateNew;
Expand Down
10 changes: 5 additions & 5 deletions src/Dax.Vpax.Obfuscator.TestApp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ private static void Obfuscate(FileInfo vpaxFile, FileInfo? dictionaryFile, strin
: obfuscator.Obfuscate(stream);

var dictionaryPath = Path.Combine(outputPath, Path.ChangeExtension(vpaxFile.Name, ".vpax.dict"));
var vpaxPath = Path.Combine(outputPath, Path.ChangeExtension(vpaxFile.Name, ".obfuscated.vpax"));
var ovpaxPath = Path.Combine(outputPath, Path.ChangeExtension(vpaxFile.Name, ".ovpax"));

dictionary.WriteTo(dictionaryPath, overwrite, indented: true);
File.WriteAllBytes(vpaxPath, stream.ToArray());
File.WriteAllBytes(ovpaxPath, stream.ToArray());
}

private static void Deobfuscate(FileInfo vpaxFile, FileInfo dictionaryFile)
private static void Deobfuscate(FileInfo ovpaxFile, FileInfo dictionaryFile)
{
var data = File.ReadAllBytes(vpaxFile.FullName);
var data = File.ReadAllBytes(ovpaxFile.FullName);
using var stream = new MemoryStream();
stream.Write(data, 0, data.Length);

var obfuscator = new VpaxObfuscator();
obfuscator.Deobfuscate(stream, ObfuscationDictionary.ReadFrom(dictionaryFile.FullName));

var vpaxPath = Path.Combine(vpaxFile.DirectoryName!, Path.ChangeExtension(vpaxFile.Name, ".deobfuscated.vpax"));
var vpaxPath = Path.Combine(ovpaxFile.DirectoryName!, Path.ChangeExtension(ovpaxFile.Name, ".deobfuscated.vpax"));
File.WriteAllBytes(vpaxPath, stream.ToArray());
}
}
Expand Down

0 comments on commit 668d33a

Please sign in to comment.