///<inheritdoc/> public string GenerateNext(long chunkSize) { var builder = new StringBuilder(); var chunkInfo = _chunkInfoBuilder.Build(chunkSize); builder.Clear(); // Generate first entry that string will be repeated var firstNumber = GenerateNumber(chunkInfo.RepeatedEntry.NumberLength); var lineToRepeat = _randomStringGenerator.Generate(chunkInfo.RepeatedEntry.LineLength); builder.AppendLine(EntryInfo.BuildEntry(firstNumber, lineToRepeat)); foreach (var entryInfo in chunkInfo.EntryInfos) { var number = GenerateNumber(entryInfo.NumberLength); var line = _randomStringGenerator.Generate(entryInfo.LineLength); builder.AppendLine(EntryInfo.BuildEntry(number, line)); } // Generate last entry with repeated string var lastNumber = GenerateNumber(chunkInfo.RepeatedEntry.NumberLength); builder.AppendLine(EntryInfo.BuildEntry(lastNumber, lineToRepeat)); return(builder.ToString()); }
public void GenerateShouldGenerateCorrectLengthString(int length, int expectedLength) { // Arrange // Act var result = _stringGenerator.Generate(length); // Assert result.Length.ShouldBe(expectedLength); }
public async Task ExecuteAsync(AddDocumentAssetCommand command, IExecutionContext executionContext) { var documentAsset = new DocumentAsset(); documentAsset.Title = command.Title; documentAsset.Description = command.Description ?? string.Empty; documentAsset.FileName = FilePathHelper.CleanFileName(command.Title); documentAsset.VerificationToken = _randomStringGenerator.Generate(6); if (string.IsNullOrWhiteSpace(documentAsset.FileName)) { throw ValidationErrorException.CreateWithProperties("Document title is empty or does not contain any safe file path characters.", nameof(command.Title)); } _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext); _entityAuditHelper.SetCreated(documentAsset, executionContext); documentAsset.FileUpdateDate = executionContext.ExecutionDate; using (var scope = _transactionScopeFactory.Create(_dbContext)) { _dbContext.DocumentAssets.Add(documentAsset); await _documentAssetCommandHelper.SaveFile(command.File, documentAsset); command.OutputDocumentAssetId = documentAsset.DocumentAssetId; scope.QueueCompletionTask(() => OnTransactionComplete(documentAsset)); await scope.CompleteAsync(); } }
/// <summary> /// Generates a unique and random security token that can be used to verify /// a request without a username and password, e.g. for a password reset link /// </summary> public string Generate() { // Get a little uniquess var guid = Guid.NewGuid().ToString().Replace("-", string.Empty); // Sprinkle some random padding var randomString = _randomStringGenerator.Generate(20); // Encrypt the lot to obfuscate it var token = Defuse.PasswordCryptographyV2.CreateHash(guid + randomString); return(token); }
public async Task ExecuteAsync(AddImageAssetCommand command, IExecutionContext executionContext) { ValidateFileType(command); var imageAsset = new ImageAsset(); imageAsset.Title = command.Title; imageAsset.FileName = SlugFormatter.ToSlug(command.Title); imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation; imageAsset.FileUpdateDate = executionContext.ExecutionDate; imageAsset.FileNameOnDisk = "file-not-saved"; imageAsset.FileExtension = "unknown"; imageAsset.VerificationToken = _randomStringGenerator.Generate(6); var fileStamp = AssetFileStampHelper.ToFileStamp(imageAsset.FileUpdateDate); _entityTagHelper.UpdateTags(imageAsset.ImageAssetTags, command.Tags, executionContext); _entityAuditHelper.SetCreated(imageAsset, executionContext); _dbContext.ImageAssets.Add(imageAsset); using (var scope = _transactionScopeFactory.Create(_dbContext)) { // Save first to get an Id await _dbContext.SaveChangesAsync(); // Update the disk filename imageAsset.FileNameOnDisk = $"{imageAsset.ImageAssetId}-{fileStamp}"; await _imageAssetFileService.SaveAsync(command.File, imageAsset, nameof(command.File)); command.OutputImageAssetId = imageAsset.ImageAssetId; scope.QueueCompletionTask(() => OnTransactionComplete(imageAsset)); await scope.CompleteAsync(); } }
private string GenerateRandomString() { const string legalCharacters = "!\"#$%&'()*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; return(_randomStringGenerator.Generate(20, legalCharacters)); }
public static HasRandomStringDecoration HasNewRandomString(this IHasId decorated, int length) { Condition.Requires(decorated).IsNotNull(); return(new HasRandomStringDecoration(decorated, _gen.Generate(length))); }