Lolly 1.4.27
Loading...
Searching...
No Matches
Enumerations | Functions
lolly::hash Namespace Reference

Enumerations

enum  sha_mode : long { SHA2_224 = 224 , SHA2_256 = 256 }
 

Functions

string md5_hexdigest (url u)
 
string sha_hexdigest (url u, sha_mode mode)
 
string sha224_hexdigest (url u)
 
string sha256_hexdigest (url u)
 
string uuid_make ()
 

Enumeration Type Documentation

◆ sha_mode

Enumerator
SHA2_224 
SHA2_256 

Definition at line 19 of file sha.hpp.

19: long { SHA2_224= 224, SHA2_256= 256 };
@ SHA2_224
Definition sha.hpp:19
@ SHA2_256
Definition sha.hpp:19

Function Documentation

◆ md5_hexdigest()

string lolly::hash::md5_hexdigest ( url u)

Definition at line 22 of file md5.cpp.

22 {
23 if (!is_local_and_single (u)) {
24 return string ("");
25 }
26 string name= as_string (u);
27 const char* path= as_charp (name);
28 if (!tb_file_access (path, TB_FILE_MODE_RO)) {
29 return string ("");
30 }
32 if (file == tb_null) {
33 return string ("");
34 }
36 if (i_size == 0) {
37 return "d41d8cd98f00b204e9800998ecf8427e";
38 }
39 tb_file_sync (file); // lock file
42 bool read_sz_equ= (real_size == i_size);
43 bool exit_suc = tb_file_exit (file); // exit file
44 if (read_sz_equ && exit_suc) {
47 if (o_size != 16) {
48 return string ("");
49 }
50
51 string md5_hex= string ();
52 for (int i= 0; i < 16; ++i) {
53 md5_hex << data::to_padded_hex (o_buffer[i]);
54 }
55 return md5_hex;
56 }
57 else {
58 return string ("");
59 }
60}
The list class represents a linked list.
Definition list.hpp:48
bool is_local_and_single(url u)
Definition file.cpp:22
char * as_charp(string s)
Definition string.cpp:294
string as_string(int16_t i)
Definition string.cpp:310

◆ sha_hexdigest()

string lolly::hash::sha_hexdigest ( url u,
sha_mode mode )

Definition at line 24 of file sha.cpp.

24 {
25 if (!is_local_and_single (u)) {
26 return string ("");
27 }
28 string name= as_string (u);
29 const char* path= as_charp (name);
30 if (!tb_file_access (path, TB_FILE_MODE_RO)) {
31 return string ("");
32 }
34 if (file == tb_null) {
35 return string ("");
36 }
38 if (i_size == 0) {
39 switch (mode) {
40 case sha_mode::SHA2_224:
41 return "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
42 case sha_mode::SHA2_256:
43 return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
44 default:
45 TM_FAILED ("Unsupported SHA mode");
46 }
47 }
48 tb_file_sync (file); // lock file
51 bool read_sz_equ= (real_size == i_size);
52 bool exit_suc = tb_file_exit (file); // exit file
53 if (read_sz_equ && exit_suc) {
55 int o_size = 32;
57 switch (mode) {
58 case sha_mode::SHA2_224:
59 o_size = 28;
61 break;
62 case sha_mode::SHA2_256:
63 o_size = 32;
65 break;
66 default:
67 TM_FAILED ("Unsupported SHA mode");
68 }
70 if (sha_size != o_size) {
71 return string ("");
72 }
73
74 string ret= string ();
75 for (int i= 0; i < o_size; ++i) {
76 ret << data::to_padded_hex (o_buffer[i]);
77 }
78 return ret;
79 }
80 else {
81 return string ("");
82 }
83}
#define TM_FAILED(msg)
Macro used to throw an exception with a specified error message.
Definition basic.hpp:93
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137

◆ sha224_hexdigest()

string lolly::hash::sha224_hexdigest ( url u)

Definition at line 86 of file sha.cpp.

86 {
87 return sha_hexdigest (u, sha_mode::SHA2_224);
88}
string sha_hexdigest(url u, sha_mode mode)
Definition sha.cpp:24

◆ sha256_hexdigest()

string lolly::hash::sha256_hexdigest ( url u)

Definition at line 91 of file sha.cpp.

91 {
92 return sha_hexdigest (u, sha_mode::SHA2_256);
93}

◆ uuid_make()

string lolly::hash::uuid_make ( )

Definition at line 20 of file uuid.cpp.

20 {
21 tb_char_t uuid[37];
23 if (ret == NULL) {
24 TM_FAILED ("Failed to generate UUID");
25 }
26 return string (ret);
27}