Home > Net >  Segmentation fault error: How to access my sha256 function?
Segmentation fault error: How to access my sha256 function?

Time:11-10

I want to get the hash value by putting the message value in the sha256 function. But I get a segmentation fault error.

This is my main function.

int main() {
    unsigned char *digest;
    unsigned char *message = "fsd";
    unsigned int len=3;
    sha256(message, len, digest);
    printf("%s",digest);    }

Below is sha256 function.

void sha256(const unsigned char *message, unsigned int len,
        unsigned char *digest);

How should I access the function so that it doesn't throw an error?

CodePudding user response:

digest needs to point somewhere. In your code digest is just an uninitialized poibter that points nowhere.

int main() {
    unsigned char digest[some_length];  // the digest will be put here

    unsigned char *message = "fsd";
    unsigned int len=3;
    sha256(message, len, digest);
    printf("%s",digest);
}

where some_length is the length needed for the SHA256 digest, refer to the documentation of the sha256 function.

Also depending on what exactly sha256 does (you didn0't provide the code nor any documentation), printf("%s",digest); won't work because digest might contain unprintable characters.

  • Related