我不知道這個問題是否很簡單,我只是不知道如何用 HashiCorp-Vault 的 Api VaultSharp 簽名,但我很絕望。
可以在此處找到帶有示例的整個文檔:https ://github.com/rajanadar/VaultSharp Encryption and Decryption 工作正常。只有簽名是一個問題。
加密代碼:
public byte[] EncryptData(byte[] data, string keyName)
{
SecretsEngine transitSecretsEngine = new SecretsEngine
{
Type = SecretsEngineType.Transit,
Path = path
};
Client.V1.System.MountSecretBackendAsync(transitSecretsEngine).Wait();
Client.V1.Secrets.Transit.CreateEncryptionKeyAsync(keyName, new CreateKeyRequestOptions()
{
Exportable = true
}, path).Wait();
EncryptRequestOptions encryptOptions = new EncryptRequestOptions
{
Base64EncodedPlainText = Convert.ToBase64String(data),
ConvergentEncryption = true,
};
Secret<EncryptionResponse> encryptionResponse = Client.V1.Secrets.Transit.EncryptAsync(keyName,
encryptOptions, path).Result;
string cipherText = encryptionResponse.Data.CipherText;
return Encoding.Unicode.GetBytes(cipherText);
}
解密代碼:
public byte[] DecryptData(string ciphertext, string keyName)
{
DecryptRequestOptions decryptOptions = new DecryptRequestOptions
{
CipherText = ciphertext,
};
Secret<DecryptionResponse> decryptionResponse = Client.V1.Secrets.Transit.DecryptAsync(keyName,
decryptOptions, path).Result;
return Convert.FromBase64String(decryptionResponse.Data.Base64EncodedPlainText);
}
這是我的簽名代碼試用版:
public byte[] Sign(byte[] plaintextBytes, string keyName)
{
byte[] hash = ComputeHash(plaintextBytes,SHA256.Create());
GCKMS.SignatureOptions options = new GCKMS.SignatureOptions()
{
Digest = Convert.ToBase64String(hash),
};
Secret<GCKMS.SignatureResponse> result = Client.V1.Secrets.GoogleCloudKMS.SignAsync(keyName,
options).Result;
return Encoding.Unicode.GetBytes(result.Data.Signature);
}
錯誤是:
VaultSharp.Core.VaultApiException: {"errors":["no handler for route 'gcpkms/sign/Manuel'"]}
最後但並非最不重要的是我的驗證簽名的代碼:
public bool ValidateSignature(byte[] plaintextByte, byte[] signature, string keyName)
{
GCKMS.VerificationOptions option = new GCKMS.VerificationOptions
{
Digest = Encoding.Unicode.GetString(ComputeHash(plaintextByte)),
Signature = Encoding.Unicode.GetString(signature)
};
Secret<GCKMS.VerificationResponse> result =
Client.V1.Secrets.GoogleCloudKMS.VerifyAsync(keyName, option).Result;
return result.Data.Valid;
}
我不確定,但這可能是因為我沒有使用帶有路徑的 SecretsEngine。我找不到任何適用於 GoogleCloudKms 的 SecretsEngine。
有用信息:
我用Guid.NewGuid().ToString();
.
ComputeHash 是一個自寫的函數,它使用給定的算法計算哈希。默認算法是 SHA256。
GCMS 是命名空間的簡短版本VaultSharp.V1.SecretsEngines.GoogleCloudKMS
非常歡迎任何想法和建議。
提前致謝!
儘管 Vault 通過 Transit 提供了方便的簽名,但您使用的 C# 包裝器不支持它。
Google KMS 確實提供簽名,但它的接口更複雜:您必須自己進行哈希處理並跟踪密鑰版本。
我的建議是你在你的 API 包裝器上玩一個把戲:
在將數據發送到 Vault 之前,您仍然必須對數據進行 base64 處理,以避免二進制編碼問題。
所以假設:
StackOverflow
transit
my-key
這應該讓你開始:
var value = new Dictionary<string, object> { "input", Convert.ToBase64String(Encoding.UTF8.GetBytes("StackOverflow")) } };
var writtenValue = await vaultClient.V1.Secrets.KeyValue.V1.WriteSecretAsync("sign/my-key", value, "transit");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句