I have the following string stored. 16 Bytes for 1-F and 4 nullBytes at the end.
e.g. 1234567890ABCDEF0000
unsigned char input[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x00, 0x00, 0x00, 0x00};
How do I get the 10 Byte Binary of this?
EDIT:
Im trying to use the SHA1 function of the openssl crypto library properly. I have the task to read a "salt" and a "password" from the command line.
Then add them together such that I have "salt" + "|" + "password".
If no salt is passed, the salt is just "\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0" which is 10 bytes right? but if a salt is passed it could be "1234567890ABCDEF"
I then have to fill this up to the right with null Bytes, so that i have 10 bytes in total But the "1234567890ABCDEF" is already 16 Bytes so i have to convert it. I dont know, I'm really struggling with the memory part in c
The easiest could be to:
Create a 0-initialized array of 10 bytes:
unsigned char salt[10] = { 0 };
then read in the hexdigits bytewise with sscanf()
:
sscanf(input, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
&salt[0], &salt[1], &salt[2], &salt[3], &salt[4],
&salt[5], &salt[6], &salt[7], &salt[8], &salt[9]);
This will convert as many bytes as needed; if only 6 hexdigits are given as salt, the first three bytes are filled and the rest remains 0.
This should do what you expect.
Hey I didn't get much from your example, but what you describe as bellow + the constrains could be solved like this. See snippet.
If no salt is passed, the salt is just "\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0" which is 10 bytes right? but if a salt is passed it could be "1234567890ABCDEF"
#include <stdio.h>
#include <string.h>
#define SALT_MAX_BYTES 10
int main(int argc, char *argv[]) {
// Init the whole array with 0-s, which is the same value as '\0'
char salt[SALT_MAX_BYTES] = {0};
// Here get the input, now assuming ./a.out [salt]
if (argc > 1) // The executable name is always passed
{
printf("Input: %s\n", argv[1]);
// Assuming ASCII...
// Assuming you want to use the ASCII value representation of input "42"
// and not the number 42 ...
strncpy(salt, argv[1], SALT_MAX_BYTES);
// Note: from here on you must strictly handle salt as length terminated.
// => input may have more then SALT_MAX_BYTES
}
else
{
puts("Usage: ...");
return -1;
}
// Left aligned output, showing nothing for \0 bytes...
printf("Entered salt is : <%-*.*s>\n", SALT_MAX_BYTES, SALT_MAX_BYTES, salt);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.