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