File: LIBRHASH.md

package info (click to toggle)
rhash 1.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,408 kB
  • sloc: ansic: 19,132; sh: 1,196; xml: 933; makefile: 662; python: 431; java: 364; cs: 288; perl: 196; ruby: 76; sed: 16
file content (125 lines) | stat: -rw-r--r-- 3,215 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
LibRHash Library
================

**LibRHash** is a professional, portable, thread-safe *C* library for computing a wide variety of hash functions.

### Main features
* Small and easy to learn interface.
* Hi-level and Low-level API.
* Calculates several hash functions simultaneously in one pass.
* Extremely portable: works the same on Linux, Unix, macOS or Windows.
* Written in pure C, small in size, open source.

Scripting Languages Support
---------------------------

LibRHash has [bindings] to several programming languages: *Java*, *C#*, *Perl*, *PHP*, *Python*, *Ruby*.

License
-------

The library is licensed under [BSD Zero Clause License].

Usage examples
--------------

### Low-level interface

* Calculating MD4 and MD5 digests simultaneously of a million characters of 'a'

```c
 #include "rhash.h" /* LibRHash interface */
 
 int main(int argc, char *argv[]) {
   rhash context;
   char digest[64];
   char output[130];
   int i;
 
   rhash_library_init(); /* initialize static data */
 
   context = rhash_init(RHASH_MD4 | RHASH_MD5);
   if(!context) {
     fprintf(stderr, "error: couldn't initialize rhash context\n");
     return 1;
   }
 
   for(i = 0; i < 1000000; i++) {
     rhash_update(context, "a", 1);
   }
   rhash_final(context, NULL); /* finalize message digests calculation */
 
   /* output message digest as a hexadecimal string */
   rhash_print(output, context, RHASH_MD4, RHPR_UPPERCASE);
   printf("%s ('a'x1000000) = %s\n", rhash_get_name(RHASH_MD4), output);
 
   rhash_print(output, context, RHASH_MD5, RHPR_UPPERCASE);
   printf("%s ('a'x1000000) = %s\n", rhash_get_name(RHASH_MD5), output);
 
   rhash_free(context);
   return 0;
 }
```

### Hi-level interface

* Calculating SHA1 message digest of a string

```c
 #include <string.h>
 #include "rhash.h" /* LibRHash interface */
 
 int main(int argc, char *argv[])
 {
   const char* msg = "message digest";
   char digest[64];
   char output[130];
 
   rhash_library_init(); /* initialize static data */
 
   int res = rhash_msg(RHASH_SHA1, msg, strlen(msg), digest);
   if(res < 0) {
     fprintf(stderr, "message digest calculation error\n");
     return 1;
   }
 
   /* convert binary digest to hexadecimal string */
   rhash_print_bytes(output, digest, rhash_get_digest_size(RHASH_SHA1),
       (RHPR_HEX | RHPR_UPPERCASE));
 
   printf("%s (\"%s\") = %s\n", rhash_get_name(RHASH_SHA1), msg, output);
   return 0;
 }
```

* Calculating TTH message digest of a file

```c
 #include <errno.h>
 #include "rhash.h" /* LibRHash interface */
 
 int main(int argc, char *argv[])
 {
   const char* filepath = "test_file.txt";
   char digest[64];
   char output[130];
 
   rhash_library_init(); /* initialize static data */
 
   int res = rhash_file(RHASH_TTH, filepath, digest);
   if(res < 0) {
     fprintf(stderr, "LibRHash error: %s: %s\n", filepath, strerror(errno));
     return 1;
   }
 
   /* convert binary digest to hexadecimal string */
   rhash_print_bytes(output, digest, rhash_get_digest_size(RHASH_TTH),
       (RHPR_BASE32 | RHPR_UPPERCASE));
 
   printf("%s (%s) = %s\n", rhash_get_name(RHASH_TTH), filepath, output);
   return 0;
 }
```

[bindings]: ../bindings/
[BSD Zero Clause License]: ../COPYING