/// <summary> /// Get my private key /// </summary> /// <param name="keyType">Key type</param> /// <param name="me">Me</param> /// <returns>Private key</returns> public async Task <string> GetPrivateKeyAsync(KeyTypeEnum keyType) { this.logger.LogDebug($"Start load {keyType.ToString()} private key from MemoryCache..."); try { var key = this.memoryCache.Get <CipherKey>(CacheKeyFactory.GetKeyCipher(keyType)); if (key != null) { this.logger.LogDebug($"Successfully load {keyType.ToString()} private key from MemoryCache. {key.ToString()}"); return(await Task.FromResult(key?.PrivateKey)); } else { this.logger.LogWarning($"Failed to load {keyType.ToString()} private key from MemoryCache."); throw new NullReferenceException($"No available {keyType.ToString()} private key"); } } catch (Exception ex) { this.logger.LogError(ex, $"{nameof(KeyManager)} error"); await this.showAllSavedKeyTypes(); return(string.Empty); } }
/// <summary> /// Get current working key /// </summary> /// <param name="keyType">Key type</param> /// <returns>CipherKey object</returns> public async Task <CipherKey> GetKeyAsync(KeyTypeEnum keyType) { this.logger.LogDebug($"Start load my {keyType.ToString()} key from MemoryCache."); try { var key = this.memoryCache.Get <CipherKey>(CacheKeyFactory.GetKeyCipher(keyType)); this.logger.LogDebug($"Successfully load {keyType.ToString()} key from MemoryCache. {key.ToString()}"); return(await Task.FromResult(key)); } catch (Exception ex) { this.logger.LogError(ex, $"{nameof(KeyManager)} error"); await this.showAllSavedKeyTypes(); return(null); } }
/// <summary> /// Key for Secret key /// </summary> /// <return>Cache key</return> public static string GetKeyCipher(KeyTypeEnum keyType) => $"{KeyPrefixCipher}-{keyType.ToString()}";
private async Task SaveKeyAsync <T>(KeyTypeEnum keyType, T key) { this.memoryCache.Set(CacheKeyFactory.GetKeyCipher(keyType), key); this.logger.LogDebug($"Successfully sync {keyType.ToString()} key(s) from KMS."); await Task.CompletedTask; }