MCPcopy Index your code
hub / github.com/nodejs/node / macImportKey

Function macImportKey

lib/internal/crypto/mac.js:105–166  ·  view source on GitHub ↗
(
  format,
  keyData,
  algorithm,
  extractable,
  usages,
)

Source from the content-addressed store, hash-verified

103}
104
105function macImportKey(
106 format,
107 keyData,
108 algorithm,
109 extractable,
110 usages,
111) {
112 const isHmac = algorithm.name === 'HMAC';
113 const usagesSet = validateKeyUsages(
114 usages, kUsages, algorithm.name);
115 let handle;
116 let length;
117 switch (format) {
118 case 'KeyObjectHandle': {
119 handle = keyData;
120 break;
121 }
122 case 'raw-secret':
123 case 'raw': {
124 if (format === 'raw' && !isHmac) {
125 return undefined;
126 }
127 handle = importSecretKey(keyData);
128 break;
129 }
130 case 'jwk': {
131 validateJwk(keyData, 'oct', extractable, usagesSet, 'sig');
132
133 if (keyData.alg !== undefined) {
134 const expected = isHmac ?
135 normalizeHashName(algorithm.hash.name, normalizeHashName.kContextJwkHmac) :
136 `K${StringPrototypeSubstring(algorithm.name, 4)}`;
137 if (expected && keyData.alg !== expected)
138 throw lazyDOMException(
139 'JWK "alg" does not match the requested algorithm',
140 'DataError');
141 }
142
143 handle = importJwkSecretKey(keyData);
144 break;
145 }
146 default:
147 return undefined;
148 }
149
150 ({ handle, length } = normalizeKeyLength(handle, algorithm)); // eslint-disable-line prefer-const
151
152 const algorithmObject = {
153 name: algorithm.name,
154 length,
155 };
156
157 if (isHmac) {
158 algorithmObject.hash = algorithm.hash;
159 }
160
161 return new InternalCryptoKey(
162 handle,

Callers

nothing calls this directly

Calls 8

validateKeyUsagesFunction · 0.85
importSecretKeyFunction · 0.85
validateJwkFunction · 0.85
normalizeHashNameFunction · 0.85
lazyDOMExceptionFunction · 0.85
importJwkSecretKeyFunction · 0.85
normalizeKeyLengthFunction · 0.85
getUsagesMaskFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…