Skip to content

Instantly share code, notes, and snippets.

@kosmolot
Last active July 19, 2017 15:44
Show Gist options
  • Save kosmolot/6e6e14d8386493eb7c6829f0eb4ac8e7 to your computer and use it in GitHub Desktop.
Save kosmolot/6e6e14d8386493eb7c6829f0eb4ac8e7 to your computer and use it in GitHub Desktop.
A Vala GCrypt.Hash example
using GCrypt;
using Posix;
void main () {
Hash hash;
GCrypt.Error err = Hash.open(out hash, Hash.Algorithm.SHA256, Hash.Flag.SECURE);
if (err != 0) {
print("Error: %s\n", err.to_string());
Process.exit(EXIT_FAILURE);
}
string str = "Hello World!";
hash.write(str.data);
// hash.final(); // implicit in Hash.read(...)
// Hash.read(...): [CCode (array_length = false)]
unowned uchar[] ary = hash.read(Hash.Algorithm.SHA256);
size_t len = hash.get_algo().get_digest_length();
print("digest length: %zu\n", len);
string result = Base64.encode(ary[0:len]);
print("SHA256 digest: %s\n", result);
hash.close();
Process.exit(EXIT_SUCCESS);
}
// run:
// valac --pkg posix --pkg gcrypt -X -lgcrypt gcrypt-hash-example.vala
// ./gcrypt-hash-example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment