aboutsummaryrefslogtreecommitdiff
path: root/libbutl
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
parenta12216dd1ed9582fc96805189caadc2b733a4e70 (diff)
Add abbreviated_string() to sha* classes
Diffstat (limited to 'libbutl')
-rw-r--r--libbutl/sha1.mxx6
-rw-r--r--libbutl/sha256.cxx18
-rw-r--r--libbutl/sha256.mxx14
3 files changed, 29 insertions, 9 deletions
diff --git a/libbutl/sha1.mxx b/libbutl/sha1.mxx
index 5259189..7ba3c0a 100644
--- a/libbutl/sha1.mxx
+++ b/libbutl/sha1.mxx
@@ -77,6 +77,12 @@ LIBBUTL_MODEXPORT namespace butl
const char*
string () const;
+ std::string
+ abbreviated_string (std::size_t n) const
+ {
+ return std::string (string (), n < 40 ? n : 40);
+ }
+
private:
struct context // Note: identical to SHA1_CTX.
{
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);
}
}
diff --git a/libbutl/sha256.mxx b/libbutl/sha256.mxx
index 23a116a..1584be8 100644
--- a/libbutl/sha256.mxx
+++ b/libbutl/sha256.mxx
@@ -111,6 +111,12 @@ LIBBUTL_MODEXPORT namespace butl
const char*
string () const;
+ std::string
+ abbreviated_string (std::size_t n) const
+ {
+ return std::string (string (), n < 64 ? n : 64);
+ }
+
private:
struct context // Note: identical to SHA256_CTX.
{
@@ -137,10 +143,10 @@ LIBBUTL_MODEXPORT namespace butl
LIBBUTL_SYMEXPORT std::string
sha256_to_fingerprint (const std::string&);
- // Convert a fingerprint (32 colon-separated hex digit pairs) to the SHA256
- // string representation (64 lower case hex digits). Throw invalid_argument
- // if the argument is not a valid fingerprint.
+ // Convert a fingerprint (32 colon-separated hex digit pairs) to the possibly
+ // abbreviated SHA256 string representation (up to 64 lower case hex digits).
+ // Throw invalid_argument if the first argument is not a valid fingerprint.
//
LIBBUTL_SYMEXPORT std::string
- fingerprint_to_sha256 (const std::string&);
+ fingerprint_to_sha256 (const std::string&, std::size_t = 64);
}