mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I9850950db8bd84f0904ea1a413151887f52098cf
40 lines
953 B
C++
40 lines
953 B
C++
#include "platform/api/crypto.h"
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
#include "platform/base/byte_array.h"
|
|
#include "absl/strings/string_view.h"
|
|
#include "openssl/digest.h"
|
|
|
|
namespace location {
|
|
namespace nearby {
|
|
|
|
// Initialize global crypto state.
|
|
void Crypto::Init() {}
|
|
|
|
static ByteArray Hash(absl::string_view input, const EVP_MD* algo) {
|
|
unsigned int md_out_size = EVP_MAX_MD_SIZE;
|
|
uint8_t digest_buffer[EVP_MAX_MD_SIZE];
|
|
if (input.empty()) return {};
|
|
|
|
if (!EVP_Digest(input.data(), input.size(), digest_buffer, &md_out_size, algo,
|
|
nullptr))
|
|
return {};
|
|
|
|
return ByteArray{reinterpret_cast<char*>(digest_buffer), md_out_size};
|
|
}
|
|
|
|
// Return MD5 hash of input.
|
|
ByteArray Crypto::Md5(absl::string_view input) {
|
|
return Hash(input, EVP_md5());
|
|
}
|
|
|
|
// Return SHA256 hash of input.
|
|
ByteArray Crypto::Sha256(absl::string_view input) {
|
|
return Hash(input, EVP_sha256());
|
|
}
|
|
|
|
} // namespace nearby
|
|
} // namespace location
|