| 331 | */ |
| 332 | template <typename Hasher> |
| 333 | struct HMACSign |
| 334 | { |
| 335 | /// The type of Hashing algorithm |
| 336 | using hasher_type = Hasher; |
| 337 | |
| 338 | /** |
| 339 | * Signs the input using the HMAC algorithm using the |
| 340 | * provided key. |
| 341 | * |
| 342 | * Arguments: |
| 343 | * @key : The secret/key to use for the signing. |
| 344 | * Cannot be empty string. |
| 345 | * @data : The data to be signed. |
| 346 | * |
| 347 | * Exceptions: |
| 348 | * Any allocation failure will result in jwt::MemoryAllocationException |
| 349 | * being thrown. |
| 350 | */ |
| 351 | static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) |
| 352 | { |
| 353 | std::string sign; |
| 354 | sign.resize(EVP_MAX_MD_SIZE); |
| 355 | std::error_code ec{}; |
| 356 | |
| 357 | uint32_t len = 0; |
| 358 | |
| 359 | unsigned char* res = HMAC(Hasher{}(), |
| 360 | key.data(), |
| 361 | static_cast<int>(key.length()), |
| 362 | reinterpret_cast<const unsigned char*>(data.data()), |
| 363 | data.length(), |
| 364 | reinterpret_cast<unsigned char*>(&sign[0]), |
| 365 | &len); |
| 366 | if (!res) { |
| 367 | ec = AlgorithmErrc::SigningErr; |
| 368 | } |
| 369 | |
| 370 | sign.resize(len); |
| 371 | return { std::move(sign), ec }; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Verifies the JWT string against the signature using |
| 376 | * the provided key. |
| 377 | * |
| 378 | * Arguments: |
| 379 | * @key : The secret/key to use for the signing. |
| 380 | * Cannot be empty string. |
| 381 | * @head : The part of JWT encoded string representing header |
| 382 | * and the payload claims. |
| 383 | * @sign : The signature part of the JWT encoded string. |
| 384 | * |
| 385 | * Returns: |
| 386 | * verify_result_t |
| 387 | * verify_result_t::first set to true if verification succeeds. |
| 388 | * false otherwise. |
| 389 | * verify_result_t::second set to relevant error if verification fails. |
| 390 | * |
nothing calls this directly
no outgoing calls
no test coverage detected