Hello Guys,
So I need to be able to get some data from an api and to do that I need to be able to decrypt one key, unfortunately for me it is a little bit confusing and any help would be appreciated.
Here is the code :
decryptHcPartyKey(
delegatorId: string,
delegateHcPartyId: string,
encryptedHcPartyKey: string,
encryptedForDelegator: boolean = false
): Promise<{ delegatorId: string; key: CryptoKey }> {
const cacheKey =
delegatorId + "|" + delegateHcPartyId + "|" + (encryptedForDelegator ? "->" : "<-")
const res = this.hcPartyKeysCache[cacheKey]
const hcPartyKeyOwner = encryptedForDelegator ? delegatorId : delegateHcPartyId
if (res) {
return Promise.resolve(res)
} else {
const keyPair = this.RSA.rsaKeyPairs[hcPartyKeyOwner]
return (keyPair
? Promise.resolve(keyPair)
: Promise.resolve(this.RSA.loadKeyPairNotImported(hcPartyKeyOwner)).then(keyPairInJwk =>
this.cacheKeyPair(keyPairInJwk, hcPartyKeyOwner)
)
)
.then(keyPair =>
this.RSA.decrypt(keyPair.privateKey, this.utils.hex2ua(encryptedHcPartyKey))
)
.then(decryptedHcPartyKey => this.AES.importKey("raw", decryptedHcPartyKey))
.then(
decryptedImportedHcPartyKey =>
(this.hcPartyKeysCache[cacheKey] = {
delegatorId: delegatorId,
key: decryptedImportedHcPartyKey
})
)
}
}
So based on my understanding I need 3 variables for this : delegatorId: string, delegateHcPartyId: string, encryptedHcPartyKey: string, then the rest goes cloudy . from my understanding I need to provide those 3 parameters and then in the end I should get the needed key decrypted.
Now , it seems that “encryptedHcPartyKey” is in the same time hex converted so I guess that I have to do first DecodeHex(encryptedHcPartyKey) and then pass it to the RSA part . I assume that [quote]hex2ua[/quote] is in my case DecodeHex
So far [quote]hex2ua[/quote] is this :
[code] * Hex String to Uint8Array
*
- @param s
-
@returns {Uint8Array}
*/
hex2ua(s: string): Uint8Array {
var ua = new Uint8Array(s.length / 2)
s = s.toLowerCase()
for (var i = 0; i < s.length; i += 2) {
ua[i / 2] =
(s.charCodeAt(i) < 58 ? s.charCodeAt(i) - 48 : s.charCodeAt(i) - 87) * 16 +
(s.charCodeAt(i + 1) < 58 ? s.charCodeAt(i + 1) - 48 : s.charCodeAt(i + 1) - 87)
}
return ua
}[/code]
Thanks in advance.