GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-hash.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <algorithm>
31 #include <iomanip>
32 #include <sstream>
33 #include <string>
34 
35 #include "hash-wrappers.h"
36 #include "lo-error.h"
37 #include "lo-hash.h"
38 #include "oct-locbuf.h"
39 
41 
43 
44 std::string
45 hash (hash_fptr hash_fcn, const std::string& str, int result_buf_len)
46 {
47  OCTAVE_LOCAL_BUFFER (unsigned char, result_buf, result_buf_len);
48 
49  hash_fcn (str.data (), str.length (), result_buf);
50 
51  std::ostringstream buf;
52 
53  for (int i = 0; i < result_buf_len; i++)
54  buf << std::hex << std::setw (2) << std::setfill ('0')
55  << (result_buf[i] & 0xFF);
56 
57  return buf.str ();
58 }
59 
60 int md2_digest_size (void) { return octave_md2_digest_size (); }
61 int md4_digest_size (void) { return octave_md4_digest_size (); }
62 int md5_digest_size (void) { return octave_md5_digest_size (); }
63 int sha1_digest_size (void) { return octave_sha1_digest_size (); }
68 
69 std::string
70 md2_hash (const std::string& str)
71 {
73 }
74 
75 std::string
76 md4_hash (const std::string& str)
77 {
79 }
80 
81 std::string
82 md5_hash (const std::string& str)
83 {
85 }
86 
87 std::string
88 sha1_hash (const std::string& str)
89 {
91 }
92 
93 std::string
94 sha224_hash (const std::string& str)
95 {
97 }
98 
99 std::string
100 sha256_hash (const std::string& str)
101 {
103 }
104 
105 std::string
106 sha384_hash (const std::string& str)
107 {
109 }
110 
111 std::string
112 sha512_hash (const std::string& str)
113 {
115 }
116 
117 std::string
118 hash (const std::string& hash_type, const std::string& str)
119 {
120  std::string ht = hash_type;
121 
122  std::transform (ht.begin (), ht.end (), ht.begin (), ::toupper);
123 
124  if (ht == "MD2")
125  return md2_hash (str);
126  else if (ht == "MD4")
127  return md4_hash (str);
128  else if (ht == "MD5")
129  return md5_hash (str);
130  else if (ht == "SHA1")
131  return sha1_hash (str);
132  else if (ht == "SHA224")
133  return sha224_hash (str);
134  else if (ht == "SHA256")
135  return sha256_hash (str);
136  else if (ht == "SHA384")
137  return sha384_hash (str);
138  else if (ht == "SHA512")
139  return sha512_hash (str);
140  else
142  ("hash function '%s' not supported", hash_type.c_str ());
143 }
144 
145 OCTAVE_END_NAMESPACE(crypto)
OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5819
int octave_sha1_digest_size(void)
Definition: hash-wrappers.c:47
int octave_sha256_digest_size(void)
Definition: hash-wrappers.c:49
void * octave_sha384_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:90
int octave_sha224_digest_size(void)
Definition: hash-wrappers.c:48
void * octave_sha224_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:78
void * octave_sha256_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:84
int octave_sha384_digest_size(void)
Definition: hash-wrappers.c:50
int octave_md5_digest_size(void)
Definition: hash-wrappers.c:46
void * octave_sha512_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:96
void * octave_md4_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:60
int octave_sha512_digest_size(void)
Definition: hash-wrappers.c:51
int octave_md4_digest_size(void)
Definition: hash-wrappers.c:45
void * octave_md2_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:54
void * octave_md5_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:66
int octave_md2_digest_size(void)
Definition: hash-wrappers.c:44
void * octave_sha1_buffer_wrapper(const char *buf, size_t len, void *res)
Definition: hash-wrappers.c:72
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:41
std::string hash(hash_fptr hash_fcn, const std::string &str, int result_buf_len)
Definition: lo-hash.cc:45
int md5_digest_size(void)
Definition: lo-hash.cc:62
int sha512_digest_size(void)
Definition: lo-hash.cc:67
std::string md2_hash(const std::string &str)
Definition: lo-hash.cc:70
std::string sha256_hash(const std::string &str)
Definition: lo-hash.cc:100
std::string sha512_hash(const std::string &str)
Definition: lo-hash.cc:112
std::string sha384_hash(const std::string &str)
Definition: lo-hash.cc:106
int sha256_digest_size(void)
Definition: lo-hash.cc:65
int md4_digest_size(void)
Definition: lo-hash.cc:61
int sha384_digest_size(void)
Definition: lo-hash.cc:66
std::string sha224_hash(const std::string &str)
Definition: lo-hash.cc:94
std::string sha1_hash(const std::string &str)
Definition: lo-hash.cc:88
int md2_digest_size(void)
Definition: lo-hash.cc:60
std::string md4_hash(const std::string &str)
Definition: lo-hash.cc:76
int sha1_digest_size(void)
Definition: lo-hash.cc:63
std::string md5_hash(const std::string &str)
Definition: lo-hash.cc:82
int sha224_digest_size(void)
Definition: lo-hash.cc:64
void *() hash_fptr(const char *buffer, std::size_t len, void *res)
Definition: lo-hash.h:37
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44