//////////////////////////////////////////////////////////////////////////////// // Copyright 2016 Google LLC. // // This software is licensed under the terms defined in the Widevine Master // License Agreement. For a copy of this agreement, please contact // widevine-licensing@google.com. //////////////////////////////////////////////////////////////////////////////// #include "common/sha_util.h" #include #include "openssl/sha.h" namespace widevine { std::string Sha1_Hash(const std::string& message) { std::string digest; digest.resize(SHA_DIGEST_LENGTH); SHA1(reinterpret_cast(message.data()), message.size(), reinterpret_cast(&digest[0])); return digest; } std::string Sha256_Hash(const std::string& message) { std::string digest; digest.resize(SHA256_DIGEST_LENGTH); SHA256(reinterpret_cast(message.data()), message.size(), reinterpret_cast(&digest[0])); return digest; } std::string GenerateSha1Uuid(const std::string& name_space, const std::string& name) { // X.667 14 Setting the fields of a name-based UUID. // - Allocate a UUID to use as a "name space identifier" for all UUIDs // generated from names in that name space. // - Compute the 16-octet hash value of the name space identifier concatenated // with the name. SHA_CTX ctx; SHA1_Init(&ctx); SHA1_Update(&ctx, name_space.data(), name_space.length()); SHA1_Update(&ctx, name.data(), name.length()); unsigned char hash[SHA_DIGEST_LENGTH]; SHA1_Final(hash, &ctx); std::string hash_str = std::string(reinterpret_cast(hash), SHA_DIGEST_LENGTH); // - For a SHA-1 hash function, the "hash value" referenced in 14.1 shall be // octets zero to 15. std::string uuid = hash_str.substr(0, 16); // - Overwrite the four most significant bits (bits 15 through 12) of the // "VersionAndTimeHigh" field with the four-bit version number from Table 3 // of 12.2 for the hash function that was used. [Name-based SHA-1 is 5] (uuid[6] &= 0xF) |= 0x50; // - Overwrite the two most significant bits (bits 7 and 6) of the // "VariantAndClockSeqHigh" field with 1 and 0, respectively. (uuid[8] &= 0x3F) |= 0x80; return uuid; } } // namespace widevine