//==- BLAKE3.h - BLAKE3 C++ wrapper for LLVM ---------------------*- C++ -*-==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This is a C++ wrapper of the BLAKE3 C interface. // //===----------------------------------------------------------------------===// #ifndef LLVM_SUPPORT_BLAKE3_H #define LLVM_SUPPORT_BLAKE3_H #include "llvm-c/blake3.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" namespace llvm { /// The constant \p LLVM_BLAKE3_OUT_LEN provides the default output length, /// 32 bytes, which is recommended for most callers. /// /// Outputs shorter than the default length of 32 bytes (256 bits) provide /// less security. An N-bit BLAKE3 output is intended to provide N bits of /// first and second preimage resistance and N/2 bits of collision /// resistance, for any N up to 256. Longer outputs don't provide any /// additional security. /// /// Shorter BLAKE3 outputs are prefixes of longer ones. Explicitly /// requesting a short output is equivalent to truncating the default-length /// output. template using BLAKE3Result = std::array; /// A class that wraps the BLAKE3 algorithm. class BLAKE3 { public: BLAKE3() { init(); } /// Reinitialize the internal state void init() { llvm_blake3_hasher_init(&Hasher); } /// Digest more data. void update(ArrayRef Data) { llvm_blake3_hasher_update(&Hasher, Data.data(), Data.size()); } /// Digest more data. void update(StringRef Str) { llvm_blake3_hasher_update(&Hasher, Str.data(), Str.size()); } /// Finalize the hasher and put the result in \p Result. /// This doesn't modify the hasher itself, and it's possible to finalize again /// after adding more input. template void final(BLAKE3Result &Result) { llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size()); } /// Finalize the hasher and return an output of any length, given in bytes. /// This doesn't modify the hasher itself, and it's possible to finalize again /// after adding more input. template BLAKE3Result final() { BLAKE3Result Result; llvm_blake3_hasher_finalize(&Hasher, Result.data(), Result.size()); return Result; } /// Return the current output for the digested data since the last call to /// init(). /// /// Other hash functions distinguish between \p result() and \p final(), with /// \p result() allowing more calls into \p update(), but there's no // difference for the BLAKE3 hash function. template BLAKE3Result result() { return final(); } /// Returns a BLAKE3 hash for the given data. template static BLAKE3Result hash(ArrayRef Data) { BLAKE3 Hasher; Hasher.update(Data); return Hasher.final(); } private: llvm_blake3_hasher Hasher; }; /// Like \p BLAKE3 but using a class-level template parameter for specifying the /// hash size of the \p final() and \p result() functions. /// /// This is useful for using BLAKE3 as the hasher type for \p HashBuilder with /// non-default hash sizes. template class TruncatedBLAKE3 : public BLAKE3 { public: /// Finalize the hasher and put the result in \p Result. /// This doesn't modify the hasher itself, and it's possible to finalize again /// after adding more input. void final(BLAKE3Result &Result) { return BLAKE3::final(Result); } /// Finalize the hasher and return an output of any length, given in bytes. /// This doesn't modify the hasher itself, and it's possible to finalize again /// after adding more input. BLAKE3Result final() { return BLAKE3::final(); } /// Return the current output for the digested data since the last call to /// init(). /// /// Other hash functions distinguish between \p result() and \p final(), with /// \p result() allowing more calls into \p update(), but there's no // difference for the BLAKE3 hash function. BLAKE3Result result() { return BLAKE3::result(); } }; } // namespace llvm #endif