@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
53 lines (51 loc) • 2.34 kB
JavaScript
import { of } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
import { PowerShellScripts } from '../generated/powershell-scripts';
import { Crypto } from './crypto';
import { PowerShell } from './powershell';
/**
* powerShellCrypto Crypto interface class.
* @dynamic
*/
export class PowerShellCrypto {
/**
* encrypt with RSA/SHA-1
*
* @param jwk the JSON Web Key. Single string with JSON.stringify format.
* @param data the original data to hash.
* @return Observable<string> the hash generated.
*/
/**
* Creates an RSAProvider or reuses a cached one on a node and returns its JSON Web Key string
*
* @param powerShellConnection the established powerShell connection to the node
* @param powerShellSession the established powerShell session with the node
* @return Observable<string> the jwk string.
*/
static getEncryptionJWKFromNode(powerShellConnection, powerShellSession) {
const getJWKCommand = PowerShell.createCommand(PowerShellScripts.Get_EncryptionJWKOnNode);
return powerShellConnection.run(powerShellSession, getJWKCommand, { logTelemetry: true })
.pipe(map(jwk => jwk.results));
}
/**
* Creates an RSAProvider or reuses a cached one on a node and uses its jwk to encrypt data
*
* @param powerShellConnection the established powerShell connection to the node
* @param powerShellSession the established powerShell session with the node
* @param data string data to be encrypted
* @return Observable<string> the encrypted data or null if data is null.
*/
static encryptDataUsingNodeJWK(powerShellConnection, powerShellSession, data) {
if (data != null && data.length > 0) {
return PowerShellCrypto.getEncryptionJWKFromNode(powerShellConnection, powerShellSession)
.pipe(mergeMap(jwk => Crypto.encryptRsaSha1(jwk, data)));
}
else {
// Simply return an observable with a null string if the passed in data is null.
// The will make password optional scenarios more idiomatic since iif() will not be
// needed to build a different observable when the password is optional to the command.
return of(data);
}
}
}
//# sourceMappingURL=powershell-crypto.js.map