Podpisywanie i weryfikowanie danych przy użyciu klucza w usłudze Azure Key Vault za pomocą języka JavaScript
Utwórz klienta KeyClient z odpowiednimi poświadczeniami uwierzytelniania programowego, a następnie utwórz klienta CryptographyClient , aby ustawić, zaktualizować i obrócić klucz w usłudze Azure Key Vault.
Podpisywanie danych
Kilka sugestii dotyczących podpisywania danych:
- Tworzenie skrótu dużych danych przed podpisaniem
- Jednokierunkowe dane skrótu przed podpisaniem, takie jak hasła
- Małe dane dwukierunkowe mogą być podpisane bezpośrednio
Podpisywanie i weryfikowanie dużych lub jednokierunkowych danych przy użyciu klucza
Aby podpisać i zweryfikować szyfrowany komunikat, użyj następujących metod:
W przypadku komunikatów szyfrowanych:
- zaloguj się , aby podpisać skrót wiadomości. Jest to przydatne w przypadku dużych danych lub jednokierunkowych danych, takich jak hasła.
- zweryfikuj, aby zweryfikować skrót komunikatu.
import { createHash } from "crypto";
import { DefaultAzureCredential } from '@azure/identity';
import {
CryptographyClient,
KeyClient,
KnownSignatureAlgorithms
} from '@azure/keyvault-keys';
// get service client
const credential = new DefaultAzureCredential();
const serviceClient = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
// get existing key
const keyVaultKey = await serviceClient.getKey('MyRsaKey');
if (keyVaultKey?.name) {
// get encryption client with key
const cryptoClient = new CryptographyClient(keyVaultKey, credential);
// get digest
const digestableData = "MyLargeOrOneWayData";
const digest = createHash('sha256')
.update(digestableData)
.update(process.env.SYSTEM_SALT || '')
.digest();
// sign digest
const { result: signature } = await cryptoClient.sign(KnownSignatureAlgorithms.RS256, digest);
// store signed digest in database
// verify signature
const { result: verified } = await cryptoClient.verify(KnownSignatureAlgorithms.RS256, digest, signature);
console.log(`Verification ${verified ? 'succeeded' : 'failed'}.`);
}
Podpisywanie i weryfikowanie małych danych przy użyciu klucza
Aby podpisać i zweryfikować dane, użyj następujących metod:
W przypadku danych:
- signData , aby podpisać blok danych.
- verifyData , aby zweryfikować dane.
import { createHash } from "crypto";
import { DefaultAzureCredential } from '@azure/identity';
import {
CryptographyClient,
KeyClient,
KnownSignatureAlgorithms
} from '@azure/keyvault-keys';
// get service client
const credential = new DefaultAzureCredential();
const serviceClient = new KeyClient(
`https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.net`,
credential
);
// get existing key
const keyVaultKey = await serviceClient.getKey('MyRsaKey');
if (keyVaultKey?.name) {
// get encryption client with key
const cryptoClient = new CryptographyClient(keyVaultKey, credential);
const data = 'Hello you bright big beautiful world!';
// sign
const { result: signature } = await cryptoClient.signData(
KnownSignatureAlgorithms.RS256,
Buffer.from(data, 'utf8')
);
// verify signature
const { result: verified } = await cryptoClient.verifyData(
KnownSignatureAlgorithms.RS256,
Buffer.from(data, 'utf8'),
signature
);
console.log(`Verification ${verified ? 'succeeded' : 'failed'}.`);
}