summaryrefslogtreecommitdiff
path: root/src/mongo/crypto/sha_block.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/crypto/sha_block.h')
-rw-r--r--src/mongo/crypto/sha_block.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/mongo/crypto/sha_block.h b/src/mongo/crypto/sha_block.h
index 45be68dc753..331b865c371 100644
--- a/src/mongo/crypto/sha_block.h
+++ b/src/mongo/crypto/sha_block.h
@@ -40,7 +40,9 @@
#include "mongo/base/status_with.h"
#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/bson/util/builder.h"
#include "mongo/util/base64.h"
+#include "mongo/util/hex.h"
#include "mongo/util/secure_compare_memory.h"
namespace mongo {
@@ -78,6 +80,20 @@ public:
return SHABlock(newHash);
}
+ static StatusWith<SHABlock> fromHexStringNoThrow(StringData hex) {
+ if (!isValidHex(hex)) {
+ return {ErrorCodes::BadValue, "Hash input is not a hex string"};
+ }
+
+ BufBuilder buf;
+ mongo::fromHexString(hex, &buf);
+ return fromBuffer(reinterpret_cast<const uint8_t*>(buf.buf()), buf.len());
+ }
+
+ static SHABlock fromHexString(StringData hex) {
+ return uassertStatusOK(fromHexStringNoThrow(hex));
+ }
+
/**
* Computes a hash of 'input' from multiple contigous buffers.
*/
@@ -188,6 +204,13 @@ public:
return base64::encode(reinterpret_cast<const char*>(_hash.data()), _hash.size());
}
+ /**
+ * Hex encoded hash block.
+ */
+ std::string toHexString() const {
+ return toHex(_hash.data(), _hash.size());
+ }
+
bool operator==(const SHABlock& other) const {
return consttimeMemEqual(this->_hash.data(), other._hash.data(), kHashLength);
}
@@ -196,6 +219,10 @@ public:
return !(*this == other);
}
+ bool operator<(const SHABlock& other) const {
+ return this->_hash < other._hash;
+ }
+
/**
* Custom hasher so SHABlocks can be used in unordered data structures.
*