|
5 | 5 | using Microsoft.AspNetCore.Mvc; |
6 | 6 | using Microsoft.Extensions.Logging; |
7 | 7 | using System; |
| 8 | +using System.Security.Cryptography; |
| 9 | +using System.Text; |
8 | 10 | using System.Collections.Generic; |
9 | 11 | using System.Threading.Tasks; |
10 | 12 | using VideoSharingService.Data.DTOs; |
@@ -34,6 +36,19 @@ private string MaskEmail(string email) |
34 | 36 | : prefix.Substring(0, 1) + new string('*', prefix.Length - 2) + prefix.Substring(prefix.Length - 1, 1); |
35 | 37 | return $"{maskedPrefix}@{domain}"; |
36 | 38 | } |
| 39 | + |
| 40 | + // Hash email address using SHA256 for logging (not reversible) |
| 41 | + private string HashEmail(string email) |
| 42 | + { |
| 43 | + if (string.IsNullOrEmpty(email)) |
| 44 | + return "unknown"; |
| 45 | + |
| 46 | + using (SHA256 sha256Hash = SHA256.Create()) |
| 47 | + { |
| 48 | + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(email)); |
| 49 | + return Convert.ToBase64String(bytes); |
| 50 | + } |
| 51 | + } |
37 | 52 | private readonly IUnitOfWork _unitOfWork; |
38 | 53 | private readonly ILogger<UserController> _logger; |
39 | 54 | private readonly IMapper _mapper; |
@@ -133,7 +148,7 @@ public async Task<IActionResult> Register([FromBody] CreateUserDTO userDTO) |
133 | 148 | public async Task<IActionResult> Login([FromBody] LoginDTO userDTO) |
134 | 149 | { |
135 | 150 | var sanitizedEmail = userDTO.Email?.Replace("\r", "").Replace("\n", ""); |
136 | | - _logger.LogInformation($"Login attempt for {MaskEmail(sanitizedEmail)}"); |
| 151 | + _logger.LogInformation($"Login attempt for email hash {HashEmail(sanitizedEmail)}"); |
137 | 152 | if (!ModelState.IsValid) |
138 | 153 | { |
139 | 154 | _logger.LogError($"Invalid post attempt {nameof(Login)}"); |
|
0 commit comments