Skip to content

Commit 9a24adf

Browse files
committed
Refactored xcrypt
1 parent 4b6ee63 commit 9a24adf

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

tools/xcrypt.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ static void XCrypt_DisplayUsage(const char *pName)
181181

182182
static xbool_t XCrypt_ApplyHMAC(xcrypt_args_t *pArgs, xcrypt_key_t *pKey)
183183
{
184+
if (!pArgs->bHybrid) return XTRUE;
185+
184186
char sHSKey[XSTR_MID];
185187
sHSKey[0] = XSTR_NUL;
186188

@@ -225,18 +227,26 @@ static xbool_t XCrypt_ApplyHMAC(xcrypt_args_t *pArgs, xcrypt_key_t *pKey)
225227
return XTRUE;
226228
}
227229

230+
static xbool_t XCrypt_SetKey(xcrypt_args_t *pArgs, xcrypt_key_t *pKey, size_t nLength)
231+
{
232+
xbool_t bStatus = nLength ? XTRUE : XFALSE;
233+
pKey->nLength = nLength;
234+
235+
if (pKey->eCipher == XC_AES)
236+
{
237+
bStatus = XCrypt_ApplyHMAC(pArgs, pKey);
238+
pKey->nLength = pArgs->nKeySize;
239+
}
240+
241+
return bStatus;
242+
}
243+
228244
static xbool_t XCrypt_GetKey(xcrypt_args_t *pArgs, xcrypt_key_t *pKey)
229245
{
230246
if (xstrused(pArgs->sKey))
231247
{
232-
pKey->nLength = xstrncpy(pKey->sKey, sizeof(pKey->sKey), pArgs->sKey);
233-
if (pKey->eCipher == XC_AES)
234-
{
235-
if (pArgs->bHybrid) return XCrypt_ApplyHMAC(pArgs, pKey);
236-
pKey->nLength = pArgs->nKeySize;
237-
}
238-
239-
return pKey->nLength ? XTRUE : XFALSE;
248+
size_t nLength = xstrncpy(pKey->sKey, sizeof(pKey->sKey), pArgs->sKey);
249+
return XCrypt_SetKey(pArgs, pKey, nLength);
240250
}
241251

242252
char sKey[XSTR_MID];
@@ -282,13 +292,8 @@ static xbool_t XCrypt_GetKey(xcrypt_args_t *pArgs, xcrypt_key_t *pKey)
282292
}
283293
}
284294

285-
if (pArgs->bHybrid && pKey->eCipher == XC_AES)
286-
return XCrypt_ApplyHMAC(pArgs, pKey);
287-
288-
if (pKey->eCipher == XC_AES) pKey->nLength = pArgs->nKeySize;
289-
else pKey->nLength = strlen(pKey->sKey);
290-
291-
return XTRUE;
295+
size_t nLength = strlen(pKey->sKey);
296+
return XCrypt_SetKey(pArgs, pKey, nLength);
292297
}
293298

294299
static XSTATUS XCrypt_ValidateArgs(xcrypt_args_t *pArgs)
@@ -308,7 +313,7 @@ static XSTATUS XCrypt_ValidateArgs(xcrypt_args_t *pArgs)
308313
if ((!pArgs->bPrint &&
309314
!xstrused(pArgs->sOutput)) ||
310315
(!xstrused(pArgs->sText) &&
311-
!xstrused(pArgs->sInput))) return XSTDNON;
316+
!xstrused(pArgs->sInput))) return XSTDNON;
312317

313318
if (XPath_Exists(pArgs->sOutput) && pArgs->bForce == XFALSE)
314319
{

0 commit comments

Comments
 (0)