private async Task <StoredPassword> HashPassword(string password) { var target = hashingOptions.Value.Target; var salt = await hasher.GenerateSalt(target.SaltLength.Value); var pwHash = await hasher.Hash(password.ToBytes(), salt, target); return(StoredPassword.From(pwHash, salt, target)); }
async Task <bool> PasswordMatches(string password, PasswordAuthentication auth) { var data = StoredPassword.Deserialize(auth.EncodedStoredPassword); var passwordBytes = password.ToBytes(); var hash = await hasher.Hash(passwordBytes, data.Salt, data.Options); if (!data.Hash.ArrayEquals(hash)) { return(false); } // TODO automatically upgrade algorithm here if it's outdated return(true); }
public Package LoadPackage() { // TODO: Consider making a singleton instance of this var ssisApp = new Application(); Package package = null; try { if (StoredPassword != null) { #if SQL2005 ssisApp.PackagePassword = Helper.ConvertToUnsecureString(StoredPassword); #else ssisApp.PackagePassword = StoredPassword.ConvertToUnsecureString(); #endif } switch (StorageType) { case PackageStorageType.FileSystem: #if SQL2017 || SQL2014 || SQL2012 if (string.IsNullOrWhiteSpace(ProjectPath)) { package = ssisApp.LoadPackage(ExpandedPackagePath, null); } else { string filePassword = StoredPassword == null ? null : StoredPassword.ConvertToUnsecureString(); // Read the project file into memory and release the file before opening the project. var fileMemoryStream = new MemoryStream(File.ReadAllBytes(ExpandedProjectPath)); _project = string.IsNullOrEmpty(filePassword) ? Project.OpenProject(fileMemoryStream) : Project.OpenProject(fileMemoryStream, filePassword); _project.ProtectionLevel = DTSProtectionLevel.EncryptSensitiveWithUserKey; package = Helper.LoadPackageFromProject(_project, _project.Name, PackagePath); } #else package = ssisApp.LoadPackage(ExpandedPackagePath, null); #endif break; case PackageStorageType.MSDB: package = ssisApp.LoadFromSqlServer(PackagePath, Server, null, null, null); break; case PackageStorageType.PackageStore: package = ssisApp.LoadFromDtsServer(PackagePath, Server, null); break; case PackageStorageType.SsisCatalog: #if SQL2017 || SQL2014 || SQL2012 string catalogPassword = StoredPassword == null ? null : StoredPassword.ConvertToUnsecureString(); var sqlConnectionStringBuilder = new SqlConnectionStringBuilder { DataSource = Server, InitialCatalog = "SSISDB", IntegratedSecurity = true }; var integrationServices = new IntegrationServices(new SqlConnection(sqlConnectionStringBuilder.ToString())); Catalog ssisCatalog = integrationServices.Catalogs.FirstOrDefault(); if (ssisCatalog == null) { throw new Exception("A SSIS Catalog could not be found."); } string ssisFolderName; string ssisProjectName; Helper.ParseSsisProjectPath(ProjectPath, out ssisFolderName, out ssisProjectName); CatalogFolder catalogFolder = ssisCatalog.Folders.FirstOrDefault(x => string.Compare(x.Name, ssisFolderName, StringComparison.OrdinalIgnoreCase) == 0); if (catalogFolder == null) { throw new Exception(string.Format("The catalog folder {0} could not be found.", ssisFolderName)); } ProjectInfo projectInfo = catalogFolder.Projects.FirstOrDefault(x => string.Compare(x.Name, ssisProjectName, StringComparison.OrdinalIgnoreCase) == 0); if (projectInfo == null) { throw new Exception(string.Format("The project {0} could not be found.", ssisProjectName)); } byte[] projectBytes = projectInfo.GetProjectBytes(); if (projectBytes == null || projectBytes.Length < 1) { throw new Exception(string.Format("The project {0} could not be loaded.", ssisProjectName)); } var catalogMemoryStream = new MemoryStream(projectBytes); _project = catalogPassword == null?Project.OpenProject(catalogMemoryStream) : Project.OpenProject(catalogMemoryStream, catalogPassword); _project.ProtectionLevel = DTSProtectionLevel.EncryptSensitiveWithUserKey; package = Helper.LoadPackageFromProject(_project, _project.Name, PackagePath); break; #else throw new NotSupportedException(); #endif } } catch (DtsRuntimeException dtsEx) { #if SQL2005 const string SsisPackageStoreVersion = "2005"; #elif SQL2008 const string SsisPackageStoreVersion = "2008"; #elif SQL2012 const string SsisPackageStoreVersion = "2012"; #elif SQL2014 const string SsisPackageStoreVersion = "2014"; #elif SQL2017 const string SsisPackageStoreVersion = "2017"; #endif if (StorageType == PackageStorageType.PackageStore && dtsEx.ErrorCode == HResults.DTS_E_PACKAGENOTFOUND) { throw new DtsPackageStoreException(string.Format("The package \"{0}\" couldn't be found in the SSIS {1} Package Store. Please ensure that the correct unit test engine is used when accessing the SSIS {1} Package Store.", PackagePath, SsisPackageStoreVersion)); } if (dtsEx.ErrorCode == HResults.DTS_E_LOADFROMSQLSERVER) { throw new DtsPackageStoreException(string.Format("There was an error while attempting to load the package \"{0}\" from MSDB. Please ensure the package path is valid and the correct unit test engine is used to execute the package. The current unit test engine is SSIS {1}.", PackagePath, SsisPackageStoreVersion)); } if (dtsEx.ErrorCode == HResults.DTS_E_LOADFROMXML) { throw new DtsPackageStoreException(string.Format("There was an error while attempting to load the package \"{0}\" from the file system. Please ensure the package path is valid and the correct unit test engine is used to execute the package. The current unit test engine is SSIS {1}.", PackagePath, SsisPackageStoreVersion)); } throw; } catch (KeyNotFoundException) { throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, "The package attribute is {0}, which does not reference a valid package.", PackagePath)); } _package = package; return(package); }