Skip to content

Commit

Permalink
stupid gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Sep 25, 2024
1 parent edccc49 commit 416ffdd
Show file tree
Hide file tree
Showing 3 changed files with 564 additions and 0 deletions.
58 changes: 58 additions & 0 deletions BTCPayApp.Core/Backup/AccountAwareVssClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Net;
using BTCPayApp.Core.Auth;
using BTCPayApp.VSS;
using VSSProto;

namespace BTCPayApp.Core.Backup;

public class AccountAwareVssClient : IVSSAPI
{
private readonly IVSSAPI _inner;
private readonly IAccountManager _accountManager;

public AccountAwareVssClient(IVSSAPI inner, IAccountManager accountManager)
{
_inner = inner;
_accountManager = accountManager;
}

private async Task<T> Wrap<T>(Func<Task<T>> func)
{
var retry = false;
attemptAgain:
try
{
return await func();
}
catch (HttpRequestException ex) when (ex.StatusCode is HttpStatusCode.Unauthorized && !retry)
{
await _accountManager.RefreshAccess();
retry = true;
goto attemptAgain;
}
}

public async Task<GetObjectResponse> GetObjectAsync(GetObjectRequest request,
CancellationToken cancellationToken = default)
{
return await Wrap(async () => await _inner.GetObjectAsync(request, cancellationToken));
}

public async Task<PutObjectResponse> PutObjectAsync(PutObjectRequest request,
CancellationToken cancellationToken = default)
{
return await Wrap(async () => await _inner.PutObjectAsync(request, cancellationToken));
}

public async Task<DeleteObjectResponse> DeleteObjectAsync(DeleteObjectRequest request,
CancellationToken cancellationToken = default)
{
return await Wrap(async () => await _inner.DeleteObjectAsync(request, cancellationToken));
}

public async Task<ListKeyVersionsResponse> ListKeyVersionsAsync(ListKeyVersionsRequest request,
CancellationToken cancellationToken = default)
{
return await Wrap(async () => await _inner.ListKeyVersionsAsync(request, cancellationToken));
}
}
56 changes: 56 additions & 0 deletions BTCPayApp.Core/Backup/SingleKeyDataProtector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.DataProtection;

namespace BTCPayApp.Core.Backup;

public class SingleKeyDataProtector : IDataProtector
{
private readonly byte[] _key;

public SingleKeyDataProtector(byte[] key)
{
if (key.Length != 32) // AES-256 key size
{
throw new ArgumentException("Key length must be 32 bytes.");
}

_key = key;
}

public IDataProtector CreateProtector(string purpose)
{
using var hmac = new HMACSHA256(_key);
var purposeBytes = Encoding.UTF8.GetBytes(purpose);
var key = hmac.ComputeHash(purposeBytes).Take(32).ToArray();
return new SingleKeyDataProtector(key);
}

public byte[] Protect(byte[] plaintext)
{
using var aes = Aes.Create();
aes.Key = _key;
aes.GenerateIV();

var iv = aes.IV;
var encrypted = aes.EncryptCbc(plaintext, iv);

return iv.Concat(encrypted).ToArray();
}

public byte[] Unprotect(byte[] protectedData)
{
using var aes = Aes.Create();
aes.Key = _key;

if(protectedData.Length == 0)
{
return protectedData;
}
var iv = protectedData.Take(16).ToArray();
var cipherText = protectedData.Skip(16).ToArray();

return aes.DecryptCbc(cipherText, iv);
}

}
Loading

0 comments on commit 416ffdd

Please sign in to comment.