annotate liboctave/util/lo-hash.cc @ 30564:796f54d4ddbf stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021. In all .txi and .texi files except gpl.txi and gpl.texi in the doc/liboctave and doc/interpreter directories, change the copyright to "Octave Project Developers", the same as used for other source files. Update copyright notices for 2022 (not done since 2019). For gpl.txi and gpl.texi, change the copyright notice to be "Free Software Foundation, Inc." and leave the date at 2007 only because this file only contains the text of the GPL, not anything created by the Octave Project Developers. Add Paul Thomas to contributors.in.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Dec 2021 18:22:40 -0500
parents 0a5b15007766
children e88a07dec498
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ////////////////////////////////////////////////////////////////////////
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
3 // Copyright (C) 2016-2022 The Octave Project Developers
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
4 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 // distribution or <https://octave.org/copyright/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
7 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
8 // This file is part of Octave.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
9 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
11 // under the terms of the GNU General Public License as published by
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
13 // (at your option) any later version.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
14 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
18 // GNU General Public License for more details.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
19 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
20 // You should have received a copy of the GNU General Public License
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
21 // along with Octave; see the file COPYING. If not, see
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
22 // <https://www.gnu.org/licenses/>.
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 //
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ////////////////////////////////////////////////////////////////////////
21889
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #if defined (HAVE_CONFIG_H)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 # include "config.h"
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #endif
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include <algorithm>
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 #include <iomanip>
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include <sstream>
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #include <string>
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 #include "hash-wrappers.h"
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 #include "lo-error.h"
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 #include "lo-hash.h"
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 #include "oct-locbuf.h"
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 namespace octave
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 namespace crypto
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 hash (hash_fptr hash_fcn, const std::string& str, int result_buf_len)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 OCTAVE_LOCAL_BUFFER (unsigned char, result_buf, result_buf_len);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 hash_fcn (str.data (), str.length (), result_buf);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 std::ostringstream buf;
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 for (int i = 0; i < result_buf_len; i++)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 buf << std::hex << std::setw (2) << std::setfill ('0')
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 << (result_buf[i] & 0xFF);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 return buf.str ();
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 int md2_digest_size (void) { return octave_md2_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 int md4_digest_size (void) { return octave_md4_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 int md5_digest_size (void) { return octave_md5_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 int sha1_digest_size (void) { return octave_sha1_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 int sha224_digest_size (void) { return octave_sha224_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 int sha256_digest_size (void) { return octave_sha256_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 int sha384_digest_size (void) { return octave_sha384_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 int sha512_digest_size (void) { return octave_sha512_digest_size (); }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 md2_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 return hash (octave_md2_buffer_wrapper, str, md2_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 md4_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 return hash (octave_md4_buffer_wrapper, str, md4_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 md5_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 return hash (octave_md5_buffer_wrapper, str, md5_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 sha1_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 return hash (octave_sha1_buffer_wrapper, str, sha1_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 sha224_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 return hash (octave_sha224_buffer_wrapper, str, sha224_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100 sha256_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 return hash (octave_sha256_buffer_wrapper, str, sha256_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 sha384_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 return hash (octave_sha384_buffer_wrapper, str, sha384_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 sha512_hash (const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 return hash (octave_sha512_buffer_wrapper, str, sha512_digest_size ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117 std::string
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 hash (const std::string& hash_type, const std::string& str)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 {
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 std::string ht = hash_type;
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 std::transform (ht.begin (), ht.end (), ht.begin (), ::toupper);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124 if (ht == "MD2")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
125 return md2_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
126 else if (ht == "MD4")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
127 return md4_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
128 else if (ht == "MD5")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129 return md5_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
130 else if (ht == "SHA1")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
131 return sha1_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
132 else if (ht == "SHA224")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
133 return sha224_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
134 else if (ht == "SHA256")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135 return sha256_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
136 else if (ht == "SHA384")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
137 return sha384_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
138 else if (ht == "SHA512")
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
139 return sha512_hash (str);
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
140 else
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
141 (*current_liboctave_error_handler)
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
142 ("hash function '%s' not supported", hash_type.c_str ());
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
143 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
144 }
69afa5dce885 hide gnulib hash function headers
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 }