private async Task <(bool same, byte[] hash)> WorkWithHashAsync(ByteArrayBuilder byteArrayBuilder, CancellationToken cancellationToken) { byte[] hash = null; try { var bytes = byteArrayBuilder.ToArray(); hash = HashHelpers.GenerateSha256Hash(bytes); if (File.Exists(DigestFilePath)) { var digest = await File.ReadAllBytesAsync(DigestFilePath, cancellationToken).ConfigureAwait(false); if (ByteHelpers.CompareFastUnsafe(hash, digest)) { if (File.Exists(NewFilePath)) { File.Delete(NewFilePath); } return(true, hash); } } } catch (Exception ex) { Logger.LogWarning("Failed to read digest."); Logger.LogInfo(ex); } return(false, hash); }
public Round(RoundParameters roundParameters) { RoundParameters = roundParameters; AmountCredentialIssuer = new(new(Random), 2, Random, MaxRegistrableAmount); WeightCredentialIssuer = new(new(Random), 2, Random, RegistrableWeightCredentials); AmountCredentialIssuerParameters = AmountCredentialIssuer.CredentialIssuerSecretKey.ComputeCredentialIssuerParameters(); WeightCredentialIssuerParameters = WeightCredentialIssuer.CredentialIssuerSecretKey.ComputeCredentialIssuerParameters(); Coinjoin = Transaction.Create(Network); Hash = new(HashHelpers.GenerateSha256Hash($"{Id}{MaxInputCountByAlice}{MinRegistrableAmount}{MaxRegistrableAmount}{RegistrableWeightCredentials}{AmountCredentialIssuerParameters}{WeightCredentialIssuerParameters}{FeeRate.SatoshiPerByte}")); }
public MutexIoManager(string filePath) : base(filePath) { var shortHash = HashHelpers.GenerateSha256Hash(FilePath).Substring(0, 7); // https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netframework-4.8 // On a server that is running Terminal Services, a named system mutex can have two levels of visibility. // If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. // If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. // In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. // If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". // Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, // and both are visible to all processes in the terminal server session. // That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes. Mutex = new AsyncMutex($"{FileNameWithoutExtension}-{shortHash}"); }
public Round( uint maxInputCountByAlice, Money minRegistrableAmount, Money maxRegistrableAmount, uint minRegistrableWeight, uint maxRegistrableWeight) { MaxInputCountByAlice = maxInputCountByAlice; MinRegistrableAmount = minRegistrableAmount; MaxRegistrableAmount = maxRegistrableAmount; MinRegistrableWeight = minRegistrableWeight; MaxRegistrableWeight = maxRegistrableWeight; Hash = new uint256(HashHelpers.GenerateSha256Hash($"{Id}{MaxInputCountByAlice}{MinRegistrableAmount}{MaxRegistrableAmount}{MinRegistrableWeight}{MaxRegistrableWeight}")); }
/// <param name="digestRandomIndex">Use the random index of the line to create digest faster. -1 is special value, it means the last character. If null then hash whole file.</param> public IoManager(string filePath, int?digestRandomIndex = null) { DigestRandomIndex = digestRandomIndex; OriginalFilePath = Guard.NotNullOrEmptyOrWhitespace(nameof(filePath), filePath, trim: true); OldFilePath = $"{OriginalFilePath}{OldExtension}"; NewFilePath = $"{OriginalFilePath}{NewExtension}"; DigestFilePath = $"{OriginalFilePath}{DigestExtension}"; FileName = Path.GetFileName(OriginalFilePath); var shortHash = HashHelpers.GenerateSha256Hash(OriginalFilePath).Substring(0, 7); FileNameWithoutExtension = Path.GetFileNameWithoutExtension(OriginalFilePath); // https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netframework-4.8 // On a server that is running Terminal Services, a named system mutex can have two levels of visibility. // If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. // If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created. // In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. // If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\". // Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, // and both are visible to all processes in the terminal server session. // That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes. Mutex = new AsyncMutex($"{FileNameWithoutExtension}-{shortHash}"); }
public SettingsViewModel(Global global) : base(global, "Settings") { Autocopy = Global.UiConfig?.Autocopy is true; CustomFee = Global.UiConfig?.IsCustomFee is true; // Use global config's data as default filler until the real data is filled out by the loading of the config onopen. var globalConfig = Global.Config; Network = globalConfig.Network; TorSocks5EndPoint = globalConfig.TorSocks5EndPoint.ToString(-1); UseTor = globalConfig.UseTor; SomePrivacyLevel = globalConfig.PrivacyLevelSome.ToString(); FinePrivacyLevel = globalConfig.PrivacyLevelFine.ToString(); StrongPrivacyLevel = globalConfig.PrivacyLevelStrong.ToString(); DustThreshold = globalConfig.DustThreshold.ToString(); BitcoinP2pEndPoint = globalConfig.GetP2PEndpoint().ToString(defaultPort: -1); IsModified = false; this.WhenAnyValue( x => x.Network, x => x.UseTor) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(_ => Save()); this.WhenAnyValue(x => x.Autocopy) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.Autocopy = x); this.WhenAnyValue(x => x.CustomFee) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.IsCustomFee = x); OpenConfigFileCommand = ReactiveCommand.Create(OpenConfigFile); LurkingWifeModeCommand = ReactiveCommand.CreateFromTask(async() => { Global.UiConfig.LurkingWifeMode = !LurkingWifeMode; await Global.UiConfig.ToFileAsync(); }); SetClearPinCommand = ReactiveCommand.Create(() => { var pinBoxText = PinBoxText?.Trim(); if (string.IsNullOrWhiteSpace(pinBoxText)) { PinWarningMessage = "Please provide PIN."; return; } if (pinBoxText.Length > 10) { PinWarningMessage = "PIN too long."; return; } if (pinBoxText.Any(x => !char.IsDigit(x))) { PinWarningMessage = "Invalid PIN."; return; } var uiConfigPinHash = Global.UiConfig.LockScreenPinHash; var enteredPinHash = HashHelpers.GenerateSha256Hash(pinBoxText); if (IsPinSet) { if (uiConfigPinHash != enteredPinHash) { PinWarningMessage = "Wrong PIN."; PinBoxText = string.Empty; return; } Global.UiConfig.LockScreenPinHash = string.Empty; } else { Global.UiConfig.LockScreenPinHash = enteredPinHash; } PinBoxText = string.Empty; PinWarningMessage = string.Empty; }); TextBoxLostFocusCommand = ReactiveCommand.Create(Save); }
public SettingsViewModel() : base("Settings") { Global = Locator.Current.GetService <Global>(); this.ValidateProperty(x => x.SomePrivacyLevel, ValidateSomePrivacyLevel); this.ValidateProperty(x => x.FinePrivacyLevel, ValidateFinePrivacyLevel); this.ValidateProperty(x => x.StrongPrivacyLevel, ValidateStrongPrivacyLevel); this.ValidateProperty(x => x.DustThreshold, ValidateDustThreshold); this.ValidateProperty(x => x.TorSocks5EndPoint, ValidateTorSocks5EndPoint); this.ValidateProperty(x => x.BitcoinP2pEndPoint, ValidateBitcoinP2pEndPoint); Autocopy = Global.UiConfig.Autocopy; CustomFee = Global.UiConfig.IsCustomFee; CustomChangeAddress = Global.UiConfig.IsCustomChangeAddress; var config = new Config(Global.Config.FilePath); config.LoadOrCreateDefaultFile(); Network = config.Network; TorSocks5EndPoint = config.TorSocks5EndPoint.ToString(-1); UseTor = config.UseTor; StartLocalBitcoinCoreOnStartup = config.StartLocalBitcoinCoreOnStartup; StopLocalBitcoinCoreOnShutdown = config.StopLocalBitcoinCoreOnShutdown; SomePrivacyLevel = config.PrivacyLevelSome.ToString(); FinePrivacyLevel = config.PrivacyLevelFine.ToString(); StrongPrivacyLevel = config.PrivacyLevelStrong.ToString(); DustThreshold = config.DustThreshold.ToString(); BitcoinP2pEndPoint = config.GetP2PEndpoint().ToString(defaultPort: -1); LocalBitcoinCoreDataDir = config.LocalBitcoinCoreDataDir; IsModified = !Global.Config.AreDeepEqual(config); this.WhenAnyValue( x => x.Network, x => x.UseTor, x => x.StartLocalBitcoinCoreOnStartup, x => x.StopLocalBitcoinCoreOnShutdown) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(_ => Save()); this.WhenAnyValue(x => x.Autocopy) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.Autocopy = x); this.WhenAnyValue(x => x.CustomFee) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.IsCustomFee = x); this.WhenAnyValue(x => x.CustomChangeAddress) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.IsCustomChangeAddress = x); OpenConfigFileCommand = ReactiveCommand.CreateFromTask(OpenConfigFileAsync); LurkingWifeModeCommand = ReactiveCommand.Create(() => { Global.UiConfig.LurkingWifeMode = !LurkingWifeMode; Global.UiConfig.ToFile(); }); SetClearPinCommand = ReactiveCommand.Create(() => { var pinBoxText = PinBoxText; if (string.IsNullOrEmpty(pinBoxText)) { NotificationHelpers.Error("Please provide a PIN."); return; } var trimmedPinBoxText = pinBoxText?.Trim(); if (string.IsNullOrEmpty(trimmedPinBoxText) || trimmedPinBoxText.Any(x => !char.IsDigit(x))) { NotificationHelpers.Error("Invalid PIN."); return; } if (trimmedPinBoxText.Length > 10) { NotificationHelpers.Error("PIN is too long."); return; } var uiConfigPinHash = Global.UiConfig.LockScreenPinHash; var enteredPinHash = HashHelpers.GenerateSha256Hash(trimmedPinBoxText); if (IsPinSet) { if (uiConfigPinHash != enteredPinHash) { NotificationHelpers.Error("PIN is incorrect."); PinBoxText = ""; return; } Global.UiConfig.LockScreenPinHash = ""; NotificationHelpers.Success("PIN was cleared."); } else { Global.UiConfig.LockScreenPinHash = enteredPinHash; NotificationHelpers.Success("PIN was changed."); } PinBoxText = ""; }); TextBoxLostFocusCommand = ReactiveCommand.Create(Save); Observable .Merge(OpenConfigFileCommand.ThrownExceptions) .Merge(LurkingWifeModeCommand.ThrownExceptions) .Merge(SetClearPinCommand.ThrownExceptions) .Merge(TextBoxLostFocusCommand.ThrownExceptions) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(ex => Logger.LogError(ex)); }
public static byte[] GetHashFile(string filePath) { var bytes = File.ReadAllBytes(filePath); return(HashHelpers.GenerateSha256Hash(bytes)); }
public SettingsViewModel() : base("Settings") { Global = Locator.Current.GetService <Global>(); Autocopy = Global.UiConfig?.Autocopy is true; CustomFee = Global.UiConfig?.IsCustomFee is true; // Use global config's data as default filler until the real data is filled out by the loading of the config onopen. var globalConfig = Global.Config; Network = globalConfig.Network; TorSocks5EndPoint = globalConfig.TorSocks5EndPoint.ToString(-1); UseTor = globalConfig.UseTor; StartLocalBitcoinCoreOnStartup = globalConfig.StartLocalBitcoinCoreOnStartup; StopLocalBitcoinCoreOnShutdown = globalConfig.StopLocalBitcoinCoreOnShutdown; SomePrivacyLevel = globalConfig.PrivacyLevelSome.ToString(); FinePrivacyLevel = globalConfig.PrivacyLevelFine.ToString(); StrongPrivacyLevel = globalConfig.PrivacyLevelStrong.ToString(); DustThreshold = globalConfig.DustThreshold.ToString(); BitcoinP2pEndPoint = globalConfig.GetP2PEndpoint().ToString(defaultPort: -1); LocalBitcoinCoreDataDir = globalConfig.LocalBitcoinCoreDataDir; IsModified = false; this.WhenAnyValue( x => x.Network, x => x.UseTor, x => x.StartLocalBitcoinCoreOnStartup, x => x.StopLocalBitcoinCoreOnShutdown) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(_ => Save()); this.WhenAnyValue(x => x.Autocopy) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.Autocopy = x); this.WhenAnyValue(x => x.CustomFee) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Global.UiConfig.IsCustomFee = x); OpenConfigFileCommand = ReactiveCommand.CreateFromTask(OpenConfigFileAsync); LurkingWifeModeCommand = ReactiveCommand.CreateFromTask(async() => { Global.UiConfig.LurkingWifeMode = !LurkingWifeMode; await Global.UiConfig.ToFileAsync(); }); SetClearPinCommand = ReactiveCommand.Create(() => { var pinBoxText = PinBoxText; if (string.IsNullOrEmpty(pinBoxText)) { NotificationHelpers.Error("Please provide a PIN."); return; } var trimmedPinBoxText = pinBoxText?.Trim(); if (string.IsNullOrEmpty(trimmedPinBoxText) || trimmedPinBoxText.Any(x => !char.IsDigit(x))) { NotificationHelpers.Error("Invalid PIN."); return; } if (trimmedPinBoxText.Length > 10) { NotificationHelpers.Error("PIN is too long."); return; } var uiConfigPinHash = Global.UiConfig.LockScreenPinHash; var enteredPinHash = HashHelpers.GenerateSha256Hash(trimmedPinBoxText); if (IsPinSet) { if (uiConfigPinHash != enteredPinHash) { NotificationHelpers.Error("PIN is incorrect."); PinBoxText = string.Empty; return; } Global.UiConfig.LockScreenPinHash = string.Empty; NotificationHelpers.Success("PIN cleared successfully."); } else { Global.UiConfig.LockScreenPinHash = enteredPinHash; NotificationHelpers.Success("PIN changed successfully."); } PinBoxText = string.Empty; }); TextBoxLostFocusCommand = ReactiveCommand.Create(Save); Observable .Merge(OpenConfigFileCommand.ThrownExceptions) .Merge(LurkingWifeModeCommand.ThrownExceptions) .Merge(SetClearPinCommand.ThrownExceptions) .Merge(TextBoxLostFocusCommand.ThrownExceptions) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(ex => Logger.LogError(ex)); }
public SettingsViewModel(Global global) : base(global, "Settings") { var config = new Config(Global.Config.FilePath); Autocopy = Global.UiConfig?.Autocopy is true; this.WhenAnyValue(x => x.Network) .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(async _ => { await config.LoadFileAsync(); var configBitcoinP2pEndPoint = Network == Network.Main ? config.MainNetBitcoinP2pEndPoint : (Network == Network.TestNet ? config.TestNetBitcoinP2pEndPoint : config.RegTestBitcoinP2pEndPoint); BitcoinP2pEndPoint = configBitcoinP2pEndPoint.ToString(defaultPort: -1); }); this.WhenAnyValue( x => x.Network, x => x.UseTor) .ObserveOn(RxApp.TaskpoolScheduler) .Subscribe(x => Save()); this.WhenAnyValue(x => x.Autocopy) .Subscribe(async x => { Global.UiConfig.Autocopy = x; await Global.UiConfig.ToFileAsync(); }); Dispatcher.UIThread.PostLogException(async() => { await config.LoadFileAsync(); Network = config.Network; TorSocks5EndPoint = config.TorSocks5EndPoint.ToString(-1); UseTor = config.UseTor.Value; SomePrivacyLevel = config.PrivacyLevelSome.ToString(); FinePrivacyLevel = config.PrivacyLevelFine.ToString(); StrongPrivacyLevel = config.PrivacyLevelStrong.ToString(); DustThreshold = config.DustThreshold.ToString(); IsModified = await Global.Config.CheckFileChangeAsync(); }); OpenConfigFileCommand = ReactiveCommand.Create(OpenConfigFile); LurkingWifeModeCommand = ReactiveCommand.CreateFromTask(async() => { Global.UiConfig.LurkingWifeMode = !LurkingWifeMode; await Global.UiConfig.ToFileAsync(); }); SetClearPinCommand = ReactiveCommand.Create(() => { var pinBoxText = PinBoxText?.Trim(); if (string.IsNullOrWhiteSpace(pinBoxText)) { PinWarningMessage = "Please provide PIN."; return; } if (pinBoxText.Length > 10) { PinWarningMessage = "PIN too long."; return; } if (pinBoxText.Any(x => !char.IsDigit(x))) { PinWarningMessage = "Invalid PIN."; return; } var uiConfigPinHash = Global.UiConfig.LockScreenPinHash; var enteredPinHash = HashHelpers.GenerateSha256Hash(pinBoxText); if (IsPinSet) { if (uiConfigPinHash != enteredPinHash) { PinWarningMessage = "Wrong PIN."; PinBoxText = string.Empty; return; } Global.UiConfig.LockScreenPinHash = string.Empty; } else { Global.UiConfig.LockScreenPinHash = enteredPinHash; } PinBoxText = string.Empty; PinWarningMessage = string.Empty; }); TextBoxLostFocusCommand = ReactiveCommand.Create(Save); }