summaryrefslogtreecommitdiff
path: root/src/mongo/crypto/sha1_block.cpp
diff options
context:
space:
mode:
authorJason Carey <jcarey@argv.me>2017-07-18 16:13:32 -0400
committerJason Carey <jcarey@argv.me>2017-07-25 10:48:44 -0400
commitffe9014101f7bd53ed9674b770e9371324fcdf34 (patch)
tree7f1051ee8d382d41cc56cf61ce39949b50ea8133 /src/mongo/crypto/sha1_block.cpp
parent4e27af1e980a5f5479fc26a3d616a5b56245261a (diff)
downloadmongo-ffe9014101f7bd53ed9674b770e9371324fcdf34.tar.gz
SERVER-30218 Add SHA256 Support
The addition of a SHA256Block will give us the flexibility to handle new workloads that aren't appropriate for SHA1. As part of this change, let's make the current code templatized so we can limit as strongly as possible the duplication of code between sha1 and sha256.
Diffstat (limited to 'src/mongo/crypto/sha1_block.cpp')
-rw-r--r--src/mongo/crypto/sha1_block.cpp111
1 files changed, 22 insertions, 89 deletions
diff --git a/src/mongo/crypto/sha1_block.cpp b/src/mongo/crypto/sha1_block.cpp
index f433b3ac698..2e3e0c8de84 100644
--- a/src/mongo/crypto/sha1_block.cpp
+++ b/src/mongo/crypto/sha1_block.cpp
@@ -1,104 +1,37 @@
/**
- * Copyright (C) 2017 MongoDB, Inc.
+ * Copyright (C) 2017 MongoDB Inc.
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
*
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * As a special exception, the copyright holders give permission to link the
- * code of portions of this program with the OpenSSL library under certain
- * conditions as described in each individual source file and distribute
- * linked combinations including the program with the OpenSSL library. You
- * must comply with the GNU Affero General Public License in all respects for
- * all of the code used other than as permitted herein. If you modify file(s)
- * with this exception, you may extend this exception to your version of the
- * file(s), but you are not obligated to do so. If you do not wish to do so,
- * delete this exception statement from your version. If you delete this
- * exception statement from all source files in the program, then also delete
- * it in the license file.
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects
+ * for all of the code used other than as permitted herein. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you do not
+ * wish to do so, delete this exception statement from your version. If you
+ * delete this exception statement from all source files in the program,
+ * then also delete it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/crypto/sha1_block.h"
-#include "mongo/bson/bsonmisc.h"
-#include "mongo/bson/bsonobjbuilder.h"
-#include "mongo/util/base64.h"
-#include "mongo/util/mongoutils/str.h"
-#include "mongo/util/secure_compare_memory.h"
-
namespace mongo {
-constexpr size_t SHA1Block::kHashLength;
-
-SHA1Block::SHA1Block(HashType hash) : _hash(std::move(hash)) {}
-
-StatusWith<SHA1Block> SHA1Block::fromBuffer(const uint8_t* input, size_t inputLen) {
- if (inputLen != kHashLength) {
- return {ErrorCodes::InvalidLength,
- str::stream() << "Unsupported SHA1Hash hash length: " << inputLen};
- }
-
- HashType newHash;
- memcpy(newHash.data(), input, inputLen);
- return SHA1Block(newHash);
-}
-
-StatusWith<SHA1Block> SHA1Block::fromBinData(const BSONBinData& binData) {
- if (binData.type != BinDataGeneral) {
- return {ErrorCodes::UnsupportedFormat, "SHA1Block only accepts BinDataGeneral type"};
- }
-
- if (binData.length != kHashLength) {
- return {ErrorCodes::UnsupportedFormat,
- str::stream() << "Unsupported SHA1Block hash length: " << binData.length};
- }
-
- HashType newHash;
- memcpy(newHash.data(), binData.data, binData.length);
- return SHA1Block(newHash);
-}
-
-SHA1Block SHA1Block::fromBinData(std::vector<unsigned char> bytes) {
- HashType newHash;
- invariant(bytes.size() == kHashLength);
- memcpy(newHash.data(), bytes.data(), bytes.size());
- return SHA1Block(newHash);
-}
-
-std::string SHA1Block::toString() const {
- return base64::encode(reinterpret_cast<const char*>(_hash.data()), _hash.size());
-}
-
-void SHA1Block::appendAsBinData(BSONObjBuilder& builder, StringData fieldName) const {
- builder.appendBinData(fieldName, _hash.size(), BinDataGeneral, _hash.data());
-}
-
-void SHA1Block::xorInline(const SHA1Block& other) {
- for (size_t x = 0; x < _hash.size(); x++) {
- _hash[x] ^= other._hash[x];
- }
-}
-
-bool SHA1Block::operator==(const SHA1Block& other) const {
- return consttimeMemEqual(this->_hash.data(), other._hash.data(), kHashLength);
-}
-
-bool SHA1Block::operator!=(const SHA1Block& other) const {
- return !(*this == other);
-}
-
-std::ostream& operator<<(std::ostream& os, const SHA1Block& sha1) {
- return os << sha1.toString();
-}
+constexpr StringData SHA1BlockTraits::name;
} // namespace mongo