9
0
Fork 0

digest: introduce digest_{init/update/final/length}

This will allow to move from a one at a time digest to a multi-instance
with too much impact on the code using it

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Jean-Christophe PLAGNIOL-VILLARD 2015-03-11 17:53:03 +01:00 committed by Sascha Hauer
parent 1ac61317c6
commit 804fae5d16
4 changed files with 39 additions and 18 deletions

View File

@ -39,7 +39,7 @@ static int do_digest(char *algorithm, int argc, char *argv[])
if (argc < 2)
return COMMAND_ERROR_USAGE;
hash = calloc(d->length, sizeof(unsigned char));
hash = calloc(digest_length(d), sizeof(unsigned char));
if (!hash) {
perror("calloc");
return COMMAND_ERROR_USAGE;
@ -60,7 +60,7 @@ static int do_digest(char *algorithm, int argc, char *argv[])
if (digest_file_window(d, filename, hash, start, size) < 0) {
ret = 1;
} else {
for (i = 0; i < d->length; i++)
for (i = 0; i < digest_length(d); i++)
printf("%02x", hash[i]);
printf(" %s\t0x%08llx ... 0x%08llx\n",

View File

@ -282,33 +282,33 @@ static int __check_passwd(unsigned char* passwd, size_t length, int std)
d = digest_get_by_name(PASSWD_SUM);
passwd1_sum = calloc(d->length, sizeof(unsigned char));
passwd1_sum = calloc(digest_length(d), sizeof(unsigned char));
if (!passwd1_sum)
return -ENOMEM;
passwd2_sum = calloc(d->length, sizeof(unsigned char));
passwd2_sum = calloc(digest_length(d), sizeof(unsigned char));
if (!passwd2_sum) {
ret = -ENOMEM;
goto err1;
}
d->init(d);
digest_init(d);
d->update(d, passwd, length);
digest_update(d, passwd, length);
d->final(d, passwd1_sum);
digest_final(d, passwd1_sum);
if (std)
ret = read_env_passwd(passwd2_sum, d->length);
ret = read_env_passwd(passwd2_sum, digest_length(d));
else
ret = read_default_passwd(passwd2_sum, d->length);
ret = read_default_passwd(passwd2_sum, digest_length(d));
if (ret < 0)
goto err2;
if (strncmp(passwd1_sum, passwd2_sum, d->length) == 0)
if (strncmp(passwd1_sum, passwd2_sum, digest_length(d)) == 0)
ret = 1;
err2:
@ -349,18 +349,18 @@ int set_env_passwd(unsigned char* passwd, size_t length)
d = digest_get_by_name(PASSWD_SUM);
passwd_sum = calloc(d->length, sizeof(unsigned char));
passwd_sum = calloc(digest_length(d), sizeof(unsigned char));
if (!passwd_sum)
return -ENOMEM;
d->init(d);
digest_init(d);
d->update(d, passwd, length);
digest_update(d, passwd, length);
d->final(d, passwd_sum);
digest_final(d, passwd_sum);
ret = write_env_passwd(passwd_sum, d->length);
ret = write_env_passwd(passwd_sum, digest_length(d));
free(passwd_sum);

View File

@ -84,7 +84,7 @@ int digest_file_window(struct digest *d, char *filename,
unsigned char *buf;
int flags = 0;
d->init(d);
digest_init(d);
fd = open(filename, O_RDONLY);
if (fd < 0) {
@ -128,12 +128,12 @@ int digest_file_window(struct digest *d, char *filename,
goto out_free;
}
d->update(d, buf, now);
digest_update(d, buf, now);
size -= now;
len += now;
}
d->final(d, hash);
digest_final(d, hash);
out_free:
if (flags)

View File

@ -50,4 +50,25 @@ int digest_file(struct digest *d, char *filename,
int digest_file_by_name(char *algo, char *filename,
unsigned char *hash);
static inline int digest_init(struct digest *d)
{
return d->init(d);
}
static inline int digest_update(struct digest *d, const void *data,
unsigned long len)
{
return d->update(d, data, len);
}
static inline int digest_final(struct digest *d, unsigned char *md)
{
return d->final(d, md);
}
static inline int digest_length(struct digest *d)
{
return d->length;
}
#endif /* __SH_ST_DEVICES_H__ */