public string GetMd5Hash(string value) { var hashValue = _cryptoProvider.ComputeMD5(new UTF8Encoding().GetBytes(value)); //Bit convertor return the byte to string as all caps hex values seperated by "-" return(BitConverter.ToString(hashValue).Replace("-", "").ToLowerInvariant()); }
private Guid GetKey(string featureId) { return(new Guid(_cryptographyProvider.ComputeMD5(Encoding.Unicode.GetBytes(featureId)))); }
private async Task PerformPackageInstallation(IProgress <double> progress, string target, PackageVersionInfo package, CancellationToken cancellationToken) { // Target based on if it is an archive or single assembly // zip archives are assumed to contain directory structures relative to our ProgramDataPath var extension = Path.GetExtension(package.targetFilename); var isArchive = string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase); if (target == null) { target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename); } // Download to temporary file so that, if interrupted, it won't destroy the existing installation var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions { Url = package.sourceUrl, CancellationToken = cancellationToken, Progress = progress }).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); // Validate with a checksum var packageChecksum = string.IsNullOrWhiteSpace(package.checksum) ? Guid.Empty : new Guid(package.checksum); if (!packageChecksum.Equals(Guid.Empty)) // support for legacy uploads for now { using (var stream = File.OpenRead(tempFile)) { var check = Guid.Parse(BitConverter.ToString(_cryptographyProvider.ComputeMD5(stream)).Replace("-", string.Empty)); if (check != packageChecksum) { throw new Exception(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name)); } } } cancellationToken.ThrowIfCancellationRequested(); // Success - move it to the real target try { Directory.CreateDirectory(Path.GetDirectoryName(target)); File.Copy(tempFile, target, true); //If it is an archive - write out a version file so we know what it is if (isArchive) { File.WriteAllText(target + ".ver", package.versionStr); } } catch (IOException ex) { _logger.LogError(ex, "Error attempting to move file from {TempFile} to {TargetFile}", tempFile, target); throw; } try { _fileSystem.DeleteFile(tempFile); } catch (IOException ex) { // Don't fail because of this _logger.LogError(ex, "Error deleting temp file {TempFile}", tempFile); } }
private byte[] CreateMd5(byte[] value) { return(_cryptographyProvider.ComputeMD5(value)); }