• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

example/H19-Jul-2018-5041

test/H19-Jul-2018-256240

LICENSEH A D19-Jul-20181 KiB2217

README.mdH A D19-Jul-20184.2 KiB139103

picosha2.hH A D19-Jul-201812.7 KiB378301

README.md

1# PicoSHA2 - a C++ SHA256 hash generator
2
3Copyright © 2017 okdshin
4
5## Introduction
6
7PicoSHA2 is a tiny SHA256 hash generator for C++ with following properties:
8
9- header-file only
10- no external dependencies (only uses standard C++ libraries)
11- STL-friendly
12- licensed under MIT License
13
14## Generating SHA256 hash and hash hex string
15
16```c++
17// any STL sequantial container (vector, list, dequeue...)
18std::string src_str = "The quick brown fox jumps over the lazy dog";
19
20std::vector<unsigned char> hash(picosha2::k_digest_size);
21picosha2::hash256(src_str.begin(), src_str.end(), hash.begin(), hash.end());
22
23std::string hex_str = picosha2::bytes_to_hex_string(hash.begin(), hash.end());
24```
25
26## Generating SHA256 hash and hash hex string from byte stream
27
28```c++
29picosha2::hash256_one_by_one hasher;
30...
31hasher.process(block.begin(), block.end());
32...
33hasher.finish();
34
35std::vector<unsigned char> hash(picosha2::k_digest_size);
36hasher.get_hash_bytes(hash.begin(), hash.end());
37
38std::string hex_str = picosha2::get_hash_hex_string(hasher);
39```
40
41The file `example/interactive_hasher.cpp` has more detailed information.
42
43## Generating SHA256 hash from a binary file
44
45```c++
46std::ifstream f("file.txt", std::ios::binary);
47std::vector<unsigned char> s(picosha2::k_digest_size);
48picosha2::hash256(f, s.begin(), s.end());
49```
50
51This `hash256` may use less memory than reading whole of the file.
52
53## Generating SHA256 hash hex string from std::string
54
55```c++
56std::string src_str = "The quick brown fox jumps over the lazy dog";
57std::string hash_hex_str;
58picosha2::hash256_hex_string(src_str, hash_hex_str);
59std::cout << hash_hex_str << std::endl;
60//this output is "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
61```
62
63```c++
64std::string src_str = "The quick brown fox jumps over the lazy dog";
65std::string hash_hex_str = picosha2::hash256_hex_string(src_str);
66std::cout << hash_hex_str << std::endl;
67//this output is "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
68```
69
70```c++
71std::string src_str = "The quick brown fox jumps over the lazy dog.";//add '.'
72std::string hash_hex_str = picosha2::hash256_hex_string(src_str.begin(), src_str.end());
73std::cout << hash_hex_str << std::endl;
74//this output is "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"
75```
76
77## Generating SHA256 hash hex string from byte sequence
78
79```c++
80std::vector<unsigned char> src_vect(...);
81std::string hash_hex_str;
82picosha2::hash256_hex_string(src_vect, hash_hex_str);
83```
84
85```c++
86std::vector<unsigned char> src_vect(...);
87std::string hash_hex_str = picosha2::hash256_hex_string(src_vect);
88```
89
90```c++
91unsigned char src_c_array[picosha2::k_digest_size] = {...};
92std::string hash_hex_str;
93picosha2::hash256_hex_string(src_c_array, src_c_array+picosha2::k_digest_size, hash_hex_str);
94```
95
96```c++
97unsigned char src_c_array[picosha2::k_digest_size] = {...};
98std::string hash_hex_str = picosha2::hash256_hex_string(src_c_array, src_c_array+picosha2::k_digest_size);
99```
100
101
102## Generating SHA256 hash byte sequence from STL sequential container
103
104```c++
105//any STL sequantial container (vector, list, dequeue...)
106std::string src_str = "The quick brown fox jumps over the lazy dog";
107
108//any STL sequantial containers (vector, list, dequeue...)
109std::vector<unsigned char> hash(picosha2::k_digest_size);
110
111// in: container, out: container
112picosha2::hash256(src_str, hash);
113```
114
115```c++
116//any STL sequantial container (vector, list, dequeue...)
117std::string src_str = "The quick brown fox jumps over the lazy dog";
118
119//any STL sequantial containers (vector, list, dequeue...)
120std::vector<unsigned char> hash(picosha2::k_digest_size);
121
122// in: iterator pair, out: contaner
123picosha2::hash256(src_str.begin(), src_str.end(), hash);
124```
125
126```c++
127std::string src_str = "The quick brown fox jumps over the lazy dog";
128unsigned char hash_byte_c_array[picosha2::k_digest_size];
129// in: container, out: iterator(pointer) pair
130picosha2::hash256(src_str, hash_byte_c_array, hash_byte_c_array+picosha2::k_digest_size);
131```
132
133```c++
134std::string src_str = "The quick brown fox jumps over the lazy dog";
135std::vector<unsigned char> hash(picosha2::k_digest_size);
136// in: iterator pair, out: iterator pair
137picosha2::hash256(src_str.begin(), src_str.end(), hash.begin(), hash.end());
138```
139