aboutsummaryrefslogtreecommitdiff
path: root/libbutl/sha256.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2018-01-19 00:02:21 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2018-02-08 20:49:08 +0300
commit4d30878d8efb86fd110c3693024db5da7aceb776 (patch)
treeb46ac328b499989ce27e48ca48bcc16f9f42d882 /libbutl/sha256.cxx
parenta12216dd1ed9582fc96805189caadc2b733a4e70 (diff)
Add abbreviated_string() to sha* classes
Diffstat (limited to 'libbutl/sha256.cxx')
-rw-r--r--libbutl/sha256.cxx18
1 files changed, 13 insertions, 5 deletions
diff --git a/libbutl/sha256.cxx b/libbutl/sha256.cxx
index be29871..694b331 100644
--- a/libbutl/sha256.cxx
+++ b/libbutl/sha256.cxx
@@ -122,7 +122,7 @@ namespace butl
string f;
f.reserve (n + 31);
- for (size_t i (0); i < n; ++i)
+ for (size_t i (0); i != n; ++i)
{
char c (s[i]);
if (!isxdigit (c))
@@ -138,7 +138,7 @@ namespace butl
}
string
- fingerprint_to_sha256 (const string& f)
+ fingerprint_to_sha256 (const string& f, size_t rn)
{
auto bad = []() {throw invalid_argument ("invalid fingerprint");};
@@ -146,9 +146,16 @@ namespace butl
if (n != 32 * 3 - 1)
bad ();
+ if (rn > 64)
+ rn = 64;
+
string s;
- s.reserve (64);
- for (size_t i (0); i < n; ++i)
+ s.reserve (rn);
+
+ // Note that we continue to validate the fingerprint after the result is
+ // ready.
+ //
+ for (size_t i (0); i != n; ++i)
{
char c (f[i]);
if ((i + 1) % 3 == 0)
@@ -161,7 +168,8 @@ namespace butl
if (!isxdigit (c))
bad ();
- s += lcase (c);
+ if (s.size () != rn)
+ s += lcase (c);
}
}