Lolly 1.4.27
Loading...
Searching...
No Matches
sha.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : sha.cpp
4 * DESCRIPTION: sha digest
5 * COPYRIGHT : (C) 2023 Darcy Shen
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "sha.hpp"
13#include "analyze.hpp"
14#include "file.hpp"
16#include "string.hpp"
17#include "url.hpp"
18
19#include <tbox/tbox.h>
20
21namespace lolly {
22namespace hash {
23string
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) {
41 return "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
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) {
59 o_size = 28;
61 break;
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) {
77 }
78 return ret;
79 }
80 else {
81 return string ("");
82 }
83}
84
85string
89
90string
94} // namespace hash
95} // namespace lolly
int hash(pointer ptr)
Computes a hash value for a pointer.
Definition basic.cpp:17
#define TM_FAILED(msg)
Macro used to throw an exception with a specified error message.
Definition basic.hpp:93
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
Definition url.hpp:37
bool is_local_and_single(url u)
Definition file.cpp:22
string to_padded_hex(uint8_t i)
Converts an 8-bit unsigned integer to a fixed-length (2) hex string.
Definition numeral.cpp:153
string sha224_hexdigest(url u)
Definition sha.cpp:86
string sha_hexdigest(url u, sha_mode mode)
Definition sha.cpp:24
string sha256_hexdigest(url u)
Definition sha.cpp:91
@ SHA2_224
Definition sha.hpp:19
@ SHA2_256
Definition sha.hpp:19
char * as_charp(string s)
Definition string.cpp:294
string as_string(int16_t i)
Definition string.cpp:310