GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
lo-hash.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2025 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
44std::string
45hash (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
68
69std::string
70md2_hash (const std::string& str)
71{
73}
74
75std::string
76md4_hash (const std::string& str)
77{
79}
80
81std::string
82md5_hash (const std::string& str)
83{
85}
86
87std::string
88sha1_hash (const std::string& str)
89{
91}
92
93std::string
94sha224_hash (const std::string& str)
95{
97}
98
99std::string
100sha256_hash (const std::string& str)
101{
103}
104
105std::string
106sha384_hash (const std::string& str)
107{
109}
110
111std::string
112sha512_hash (const std::string& str)
113{
115}
116
117std::string
118hash (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
145OCTAVE_END_NAMESPACE(crypto)
146OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
int octave_sha1_digest_size(void)
int octave_sha256_digest_size(void)
int octave_sha224_digest_size(void)
void * octave_sha1_buffer_wrapper(const char *buf, size_t len, void *res)
void * octave_sha384_buffer_wrapper(const char *buf, size_t len, void *res)
int octave_sha384_digest_size(void)
int octave_md5_digest_size(void)
void * octave_md5_buffer_wrapper(const char *buf, size_t len, void *res)
int octave_sha512_digest_size(void)
int octave_md4_digest_size(void)
void * octave_sha224_buffer_wrapper(const char *buf, size_t len, void *res)
void * octave_md4_buffer_wrapper(const char *buf, size_t len, void *res)
void * octave_sha256_buffer_wrapper(const char *buf, size_t len, void *res)
void * octave_md2_buffer_wrapper(const char *buf, size_t len, void *res)
void * octave_sha512_buffer_wrapper(const char *buf, size_t len, void *res)
int octave_md2_digest_size(void)
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 md2_digest_size()
Definition lo-hash.cc:60
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()
Definition lo-hash.cc:65
int md5_digest_size()
Definition lo-hash.cc:62
int sha512_digest_size()
Definition lo-hash.cc:67
int sha1_digest_size()
Definition lo-hash.cc:63
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 md4_digest_size()
Definition lo-hash.cc:61
std::string md4_hash(const std::string &str)
Definition lo-hash.cc:76
int sha384_digest_size()
Definition lo-hash.cc:66
int sha224_digest_size()
Definition lo-hash.cc:64
std::string md5_hash(const std::string &str)
Definition lo-hash.cc:82
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