public async Task <string> GenerateTokenAsync(TokenInfo info) { var secret = await service .GetSecretAsync(info.Owner) .ConfigureAwait(false); if (secret == null || secret.Value == null) { secret = Secret.Generate(info.Owner, 9); await service.AddSecretAsync(secret); } var content = JsonSerializer.Serialize( info.Content, typeof(IEnumerable <string>), new JsonSerializerOptions() { IgnoreNullValues = true }); var owner = Encoding.UTF8.GetBytes(info.Owner); this.cipher.Encrypt( Encoding.UTF8.GetBytes(secret.Value), Encoding.UTF8.GetBytes(content), owner, out var checksum, out var data); return(Token.Create(data, owner, checksum).ToString()); }
public ConfigsTFAModel() { TFA = new TFAInfo { Secret = Secret.Generate() }; IsActive = current.HasTFA; }
public byte[] Encrypt(byte[] plaintext) { Ensure.NotNullOrEmpty(plaintext, nameof(plaintext)); var iv = Secret.Generate(16); // 128 bit iv using (var aes = new AesDataProtector(key: key.Value, iv: iv.Value)) { var ciphertext = aes.Encrypt(plaintext); var message = new EncryptedDataMessage( keyId: key.Id, iv: iv.Value, ciphertext: ciphertext ); return(Serializer.Serialize(message)); } }
public void ConfigureServices(IServiceCollection services) { services.AddSingleton <IDatabaseSettings>( Configuration.GetSection(nameof(DatabaseSettings)).Get <DatabaseSettings>()); services.AddSingleton <SecretService>(); services.AddSingleton <ExoplanetService>(); services.AddSingleton <SymmetricAlgorithm>(provider => { var service = provider.GetService <SecretService>(); var secret = service.GetSecretAsync("secret") .GetAwaiter().GetResult(); if (secret == null) { secret = Secret.Generate("secret", 16); service.AddSecretAsync(secret) .GetAwaiter().GetResult(); } var alg = SymmetricAlgorithm.Create("AES"); alg.Key = Convert.FromBase64String(secret.Value); alg.Mode = CipherMode.ECB; alg.Padding = PaddingMode.Zeros; return(alg); }); services.AddSingleton <ITransformer, Transformer>(); services.AddSingleton <IMultiplicator, Multiplicator>(); services.AddSingleton <ICombiner, Combiner>(); services.AddSingleton <ICipher, Cipher>(); services.AddSingleton <Authenticator>(); services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); }