private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) { using (var hmac = new HMACSHA512()) { passwordSalt = hmac.Key; passwordHash = hmac.ComputeHash(UTF8.GetBytes(password)); } }
internal async Task ProcessSmqAsync() { await Task.Yield(); var token = this.Token; while (!token.IsCancellationRequested && this.Socket.State == WebSocketState.Open) { if (this.SocketMessageQueue.IsEmpty) { break; } if (!this.SocketMessageQueue.TryDequeue(out var message)) { break; } var buff = UTF8.GetBytes(message); var msgc = buff.Length / BUFFER_SIZE; if (buff.Length % BUFFER_SIZE != 0) { msgc++; } for (var i = 0; i < msgc; i++) { var off = BUFFER_SIZE * i; var cnt = Math.Min(BUFFER_SIZE, buff.Length - off); var lm = i == msgc - 1; await this.Socket.SendAsync(new ArraySegment <byte>(buff, off, cnt), WebSocketMessageType.Text, lm, this.Token).ConfigureAwait(false); } } }
private static void WriteString(FileStream Stream, string String) { byte[] EncodedString = UTF8.GetBytes(String ?? string.Empty); Stream.Write(GetBytes(EncodedString.Length + 1), 0, 4); Stream.Write(EncodedString, 0, EncodedString.Length); Stream.WriteByte(0); }
public static IEnumerable <CharacterInfo> GetCharacters(string s) { var bytes = UTF32.GetBytes(s); for (int i = 0; i < bytes.Length; i += 4) { var cp = new CharacterInfo(); var utf32 = new byte[4]; Array.Copy(bytes, i, utf32, 0, 4); cp.utf32 = utf32; cp.codePoint = (utf32[0] << 0) | (utf32[1] << 8) | (utf32[2] << 16) | (utf32[3] << 24); var decoded = UTF32.GetString(utf32); cp.@char = decoded; cp.utf16 = Unicode.GetBytes(decoded); cp.utf8 = UTF8.GetBytes(decoded); yield return(cp); } }
public static void Registrar(IServiceCollection services, IConfiguration configuration) { services.AddAuthentication(o => { o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(o => { o.TokenValidationParameters = new TokenValidationParameters { ValidateLifetime = true, ValidateAudience = true, ValidAudience = configuration.GetValue <string>("JwtTokenSettings:Audience"), ValidateIssuer = true, ValidIssuer = configuration.GetValue <string>("JwtTokenSettings:Issuer"), ValidateIssuerSigningKey = true, ClockSkew = TimeSpan.Zero, IssuerSigningKey = new SymmetricSecurityKey(UTF8 .GetBytes(configuration.GetValue <string>("JwtTokenSettings:IssuerSigningKey"))) }; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser() .Build()); }); }
public void ClearStorageAndWriteNextRandomValue(long nextRandomValue) { var nextRandomValueBytes = UTF8.GetBytes($"{nextRandomValue}{StorageLinesSeparator}"); _storage.Write(nextRandomValueBytes); _storageBytes = nextRandomValueBytes; }
async Task ProcessAsyncIO() { using (var stream = new FileStream("test1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE)) { Console.WriteLine($"1. Use I/O Thread : {stream.IsAsync}"); byte[] buffer = UTF8.GetBytes(CreateFileContent()); var writeTask = Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, 0, buffer.Length, null); await writeTask; } using (var stream = new FileStream("test2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE, FileOptions.Asynchronous)) { Console.WriteLine($"2. Uses I/O Thread : {stream.IsAsync}"); byte[] buffer = UTF8.GetBytes(CreateFileContent()); var writeTask = Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, 0, buffer.Length, null); await writeTask; } using (var stream = File.Create("test3.txt", BUFFER_SIZE, FileOptions.Asynchronous)) using (var sw = new StreamWriter(stream)) { Console.WriteLine($"3. Uses I/O Thread : {stream.IsAsync}"); await sw.WriteAsync(CreateFileContent()); } using (var sw = new StreamWriter("test4.txt", true)) { Console.WriteLine($"4. Uses I/O Thread : {((FileStream)sw.BaseStream).IsAsync}"); await sw.WriteAsync(CreateFileContent()); } Console.WriteLine("Starting parsing files in parallel"); var readTasks = new Task <long> [4]; for (int i = 0; i < readTasks.Length; i++) { string fileName = $"test{i + 1}.txt"; readTasks[i] = SumFileContent(fileName); } long[] sums = await Task.WhenAll(readTasks); Console.WriteLine($"Sum in all files : {sums.Sum()}"); Console.WriteLine($"Deleting files"); Task[] deleteTasks = new Task[4]; for (int i = 0; i < deleteTasks.Length; i++) { string fileName = $"test{i + 1}.txt"; deleteTasks[i] = SimulateAsuncDelete(fileName); } await Task.WhenAll(deleteTasks); Console.WriteLine("Deleting complete."); }
Task AwaitConnectionTune(IContext context) { switch (context.Message) { case (Inbound, ConnectionSecure message): { _state.AuthenticationStage++; var challenge = UTF8.GetBytes(message.Challenge); var authenticationResponse = _connectionConfiguration.AuthenticationStrategy.Respond(stage: _state.AuthenticationStage, challenge: challenge); context.Send(context.Parent, (Outbound, new ConnectionSecureOk(UTF8.GetString(authenticationResponse)))); return(Done); } case (Inbound, ConnectionTune message): { var heartbeatFrequency = Min(message.Heartbeat, _connectionConfiguration.HeartbeatFrequency); var maximumFrameSize = Min(message.FrameMax, _connectionConfiguration.MaximumFrameSize); var maximumChannelCount = Min(message.ChannelMax, _connectionConfiguration.MaximumChannelCount); context.Send(context.Parent, (Outbound, new ConnectionTuneOk( channelMax: maximumChannelCount, frameMax: maximumFrameSize, heartbeat: heartbeatFrequency ))); context.Send(context.Parent, (StartHeartbeatTransmission, frequency: heartbeatFrequency)); context.Send(context.Parent, (Outbound, new ConnectionOpen( virtualHost: _connectionConfiguration.VirtualHost ))); _behaviour.Become(AwaitConnectionOpenOk); return(Done); } default: return(Done); } }
/// <summary> /// Creates a <see cref="WaveFileWriter"/> that writes to a <see cref="Stream"/>. /// </summary> public WaveFileWriter(Stream outStream, WaveFormat format) { _ofstream = outStream; _writer = new BinaryWriter(outStream, UTF8); _writer.Write(UTF8.GetBytes("RIFF")); _writer.Write(0); // placeholder _writer.Write(UTF8.GetBytes("WAVEfmt ")); _waveFormat = format; _writer.Write(18 + format.ExtraSize); // wave format Length format.Serialize(_writer); // CreateFactChunk if (format.Encoding != WaveFormatTag.Pcm) { _writer.Write(UTF8.GetBytes("fact")); _writer.Write(4); _factSampleCountPos = outStream.Position; _writer.Write(0); // number of samples } // WriteDataChunkHeader _writer.Write(UTF8.GetBytes("data")); _dataSizePos = outStream.Position; _writer.Write(0); // placeholder Length = 0; }
private bool ValidateWsUsernameToken(WsUsernameToken wsUsernameToken) { if (wsUsernameToken.Username != _username) { return(false); } var isClearText = wsUsernameToken.Password?.Type == null || wsUsernameToken.Password.Type == _passwordTextType; if (isClearText) { return(wsUsernameToken.Password?.Value == _password); } var nonceArray = wsUsernameToken.Nonce != null ? wsUsernameToken.Nonce : Array.Empty <byte>(); var createdArray = wsUsernameToken.Created != null?UTF8.GetBytes(wsUsernameToken.Created) : Array.Empty <byte>(); var passwordArray = _password != null?UTF8.GetBytes(_password) : Array.Empty <byte>(); var hashArray = new byte[nonceArray.Length + createdArray.Length + passwordArray.Length]; Array.Copy(nonceArray, 0, hashArray, 0, nonceArray.Length); Array.Copy(createdArray, 0, hashArray, nonceArray.Length, createdArray.Length); Array.Copy(passwordArray, 0, hashArray, nonceArray.Length + createdArray.Length, passwordArray.Length); var hash = SHA1.Create().ComputeHash(hashArray); var serverPasswordDigest = ToBase64String(hash); var clientPasswordDigest = wsUsernameToken.Password?.Value; return(serverPasswordDigest == clientPasswordDigest); }
/// <summary> /// Returns the base-64 encoded, {username}:{password} formatted string of this `<see cref="Credential"/>. /// </summary> public string ToBase64String() { string basicAuthValue = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _username, _password); byte[] authBytes = UTF8.GetBytes(basicAuthValue); return(Convert.ToBase64String(authBytes)); }
public static void BodyParseSerializeTest(string[] args) { BodyParser parser = new BodyParser(); BodySerializer serial = new BodySerializer(); serial.Pipe(parser); serial.OnFail += () => Console.WriteLine("serializer failed"); parser.OnData += (data) => Console.WriteLine(UTF8.GetString(data)); parser.OnFinish += () => Console.WriteLine("parser finished"); parser.Excess.Pipe(VoidWritable.Default); BodyType bodyType = new BodyType(null, TransferEncoding.Chunked, TransferCompression.Deflate); if (!parser.TrySetFor(bodyType)) { Console.WriteLine("parser failed to set"); } if (!serial.TrySetFor(bodyType)) { Console.WriteLine("serializer failed to set"); } serial.Write(UTF8.GetBytes("I am a body\r\nxd\r\n")); serial.Write(UTF8.GetBytes("I am a body\r\nasfjaskfd\r\nasdfa")); serial.Finish(); Console.ReadKey(); }
public static string Encrypt(this string plainText) { if (IsEncrypted(plainText)) { return(plainText); } var plainTextBytes = UTF8.GetBytes(plainText); var keyBytes = new Rfc2898DeriveBytes(PASSWORD_HASH, ASCII.GetBytes(SALT_KEY)).GetBytes(256 / 8); var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros }; var encryptor = symmetricKey.CreateEncryptor(keyBytes, ASCII.GetBytes(VI_KEY)); using (var memoryStream = new MemoryStream()) { using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); return(ENCRYPTION_INDICATOR + Convert.ToBase64String(memoryStream.ToArray())); } } }
public bool SendFile(string path) { if (ConnectedSocket != null && ConnectedSocket.Connected) { try { FileInfo fi = new FileInfo(path); byte[] len = BitConverter.GetBytes(fi.Length); byte[] name = UTF8.GetBytes(fi.Name); byte[] nameLen = BitConverter.GetBytes(name.Length); byte[] head = new byte[1 + len.Length + nameLen.Length + name.Length]; head[0] = (byte)ChatType.File; Array.Copy(len, 0, head, 1, len.Length); Array.Copy(nameLen, 0, head, 1 + len.Length, nameLen.Length); Array.Copy(name, 0, head, 1 + len.Length + nameLen.Length, name.Length); ConnectedSocket.SendFile( path, head, null, TransmitFileOptions.UseDefaultWorkerThread ); return(true); } catch (Exception e) { // 连接断开了 Console.WriteLine("send file exception : " + e.Message); } } return(false); }
private static CsvReport PrepareReportFile(IEnumerable <string> titles, IEnumerable <string> values, string reportName) { var reportRows = GetAllRows(titles, values).ToList(); var bytesData = UTF8.GetBytes(string.Join(NewLine, reportRows)); return(new CsvReport(reportRows, bytesData, reportName)); }
public void PublishAdvancedEncodeTestv5() { // Arrange MqttMsgPublish publish = new(Topic, Encoding.UTF8.GetBytes(MessageString), true, MqttQoSLevel.ExactlyOnce, false); publish.MessageId = MessageId; publish.ContentType = "UTF8"; publish.IsPayloadUTF8 = true; publish.SubscriptionIdentifier = 268435454; publish.UserProperties.Add(new UserProperty("One", "prop")); publish.UserProperties.Add(new UserProperty("second", "property")); publish.CorrelationData = new byte[] { 1, 2, 3, 4, 5, 6 }; publish.ResponseTopic = "response topic"; publish.TopicAlias = 33; publish.MessageExpiryInterval = 12345; byte[] encodedCorrect = new byte[] { 60, 127, 0, 19, 116, 104, 105, 115, 116, 111, 112, 105, 99, 47, 115, 111, 109, 101, 116, 104, 105, 110, 103, 0, 42, 79, 1, 1, 2, 0, 0, 48, 57, 35, 0, 33, 8, 0, 14, 114, 101, 115, 112, 111, 110, 115, 101, 32, 116, 111, 112, 105, 99, 9, 0, 6, 1, 2, 3, 4, 5, 6, 38, 0, 3, 79, 110, 101, 0, 4, 112, 114, 111, 112, 38, 0, 6, 115, 101, 99, 111, 110, 100, 0, 8, 112, 114, 111, 112, 101, 114, 116, 121, 11, 254, 255, 255, 127, 3, 0, 4, 85, 84, 70, 56, 84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103, 32, 109, 101, 115, 115, 97, 103, 101 }; // Act byte[] encoded = publish.GetBytes(MqttProtocolVersion.Version_5); // Assert Assert.Equal(encodedCorrect, encoded); }
public async Task ThenIfFileHasMissingFieldsReturnError(string header) { var inputData = $"{header}" + @" Abba123,1113335559,Froberg,Chris,1998-12-08,SE123321C,25,,,2,2120-08,2125-08,1500,,Employer ref,Provider ref"; var textStream = new MemoryStream(UTF8.GetBytes(inputData)); _file.Setup(m => m.InputStream).Returns(textStream); BulkUploadApprenticeshipsCommand commandArgument = null; _mockMediator.Setup(x => x.Send(It.IsAny <BulkUploadApprenticeshipsCommand>(), new CancellationToken())) .ReturnsAsync(new Unit()) .Callback((object x) => commandArgument = x as BulkUploadApprenticeshipsCommand); _mockMediator.Setup(m => m.Send(It.IsAny <GetCommitmentQueryRequest>(), new CancellationToken())) .Returns(Task.FromResult(new GetCommitmentQueryResponse { Commitment = new CommitmentView { AgreementStatus = AgreementStatus.NotAgreed, EditStatus = EditStatus.ProviderOnly } })); _mockMediator.Setup(m => m.Send(It.IsAny <GetOverlappingApprenticeshipsQueryRequest>(), It.IsAny <CancellationToken>())) .Returns( Task.Run(() => new GetOverlappingApprenticeshipsQueryResponse { Overlaps = new List <ApprenticeshipOverlapValidationResult> { new ApprenticeshipOverlapValidationResult { OverlappingApprenticeships = new List <OverlappingApprenticeship> { new OverlappingApprenticeship { Apprenticeship = new Apprenticeship { ULN = "1113335559" }, ValidationFailReason = ValidationFailReason.DateEmbrace } } } } })); var model = new UploadApprenticeshipsViewModel { Attachment = _file.Object, HashedCommitmentId = "ABBA123", ProviderId = 111 }; var file = await _sut.UploadFile("user123", model, new SignInUserModel()); //Assert Assert.IsTrue(file.HasFileLevelErrors); _logger.Verify(x => x.Info(It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <long?>()), Times.Once); _logger.Verify(x => x.Error(It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <long?>(), It.IsAny <long?>(), It.IsAny <long?>()), Times.Never); }
public static (byte[] passwordHash, byte[] passwordSalt) CreatePasswordHash(string password) { using var hmac = new HMACSHA512(); // Generates a random key (salt) var passwordSalt = hmac.Key; var passwordHash = hmac.ComputeHash(UTF8.GetBytes(password)); return(passwordHash, passwordSalt); }
/// <summary> /// Computes the FNV-1a 256-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 256-bit hash of the specified data.</returns> /// <exception cref="AssertFailedException">Thrown if expected is not equal to actual.</exception> // ReSharper disable once InconsistentNaming private string Fnv1a256(string data) { AreEqual(256, this._alg.HashSize); string value = new BigInteger(this._alg.ComputeHash(UTF8.GetBytes(data)).AddZero()).ToString("X64", InvariantCulture); return(value.Substring(value.Length - 64)); }
public async static Task SendAsString(this ClientWebSocket client, string message) { var byteMessage = UTF8.GetBytes(message); var segment = new ArraySegment <byte>(byteMessage); await client.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None); }
private protected override void ImplWriteString(ref State state, string value, int expectedBytes) { DemandSpace(expectedBytes, this, ref state); int actualBytes = UTF8.GetBytes(value, 0, value.Length, ioBuffer, ioIndex); ioIndex += actualBytes; Helpers.DebugAssert(expectedBytes == actualBytes); }
/// <summary> /// Computes the FNV-1a 64-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 64-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static string Fnv1a64s(this string data) { using (HashAlgorithm alg = new Fnv1a64()) { return("0x" + ((ulong)ToInt64(alg.ComputeHash(UTF8.GetBytes(data)), 0)).ToString("X16", InvariantCulture)); } }
/// <summary> /// Computes the FNV-1a 32-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 32-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static string Fnv1a32s(this string data) { using (HashAlgorithm alg = new Fnv1a32()) { return("0x" + ((uint)ToInt32(alg.ComputeHash(UTF8.GetBytes(data)), 0)).ToString("X8", InvariantCulture)); } }
public static int WriteNullTerminatedString(this BinaryWriter writer, string value) { writer.Align(); var position = (int)writer.BaseStream.Position; writer.Write(UTF8.GetBytes(value + '\0')); return(position); }
/// <summary> /// Asynchronously computes the FNV-1a 64-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>An asynchronous <see cref="Task{TResult}"/> containing the FNV-1a 64-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static async Task <string> Fnv1a64sAsync(this string data) { using (HashAlgorithm alg = new Fnv1a64()) { return(await Task.FromResult("0x" + ((ulong)ToInt64(alg.ComputeHash(UTF8.GetBytes(data)), 0)).ToString("X16", InvariantCulture)).ConfigureAwait(false)); } }
private static async Task WriteEventType(Stream stream, string type) { await stream.WriteAll(_eventLabel).ConfigureAwait(false); var bytes = UTF8.GetBytes(type); await stream.WriteAll(bytes).ConfigureAwait(false); await stream.WriteLineFeed().ConfigureAwait(false); }
private void AddToStorage(string value) { var valueBytes = UTF8.GetBytes($"{value}{StorageLinesSeparator}"); var bytesWithNewValue = _storageBytes.Concat(valueBytes).ToArray(); _storage.Write(bytesWithNewValue); _storageBytes = bytesWithNewValue; }
public Blob valueOf(string o) { var bytes = o == null ? null : UTF8.GetBytes(o); var inst = new BlobInstance { BlobData = bytes }; return(new Blob(inst)); }
private static string HashText(string text) { var textBytes = UTF8.GetBytes(text); var sha = new SHA512CryptoServiceProvider(); var hash = sha.ComputeHash(textBytes); return(System.Convert.ToBase64String(hash)); }
/// <summary> /// Computes the FNV-1a 256-bit hash for the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>The FNV-1a 256-bit hash of the specified data.</returns> // ReSharper disable once InconsistentNaming private static string Fnv1a256s(this string data) { using (HashAlgorithm alg = new Fnv1a256()) { string value = new BigInteger(alg.ComputeHash(UTF8.GetBytes(data)).AddZero()).ToString("X64", InvariantCulture); return("0x" + value.Substring(value.Length - 64)); } }