ladybird/Userland/Libraries/LibCrypt/crypt.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

71 lines
1.8 KiB
C++

/*
* Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Base64.h>
#include <AK/Types.h>
#include <LibCrypto/Hash/SHA2.h>
#include <crypt.h>
#include <errno.h>
#include <string.h>
extern "C" {
static struct crypt_data crypt_data;
char* crypt(char const* key, char const* salt)
{
crypt_data.initialized = true;
return crypt_r(key, salt, &crypt_data);
}
static constexpr size_t crypt_salt_max = 16;
char* crypt_r(char const* key, char const* salt, struct crypt_data* data)
{
if (!data->initialized) {
errno = EINVAL;
return nullptr;
}
// We only support SHA-256 at the moment
if (salt[0] != '$' || salt[1] != '5') {
errno = EINVAL;
return nullptr;
}
char const* salt_value = salt + 3;
size_t salt_len = min(strcspn(salt_value, "$"), crypt_salt_max);
size_t header_len = salt_len + 3;
bool fits = ByteString(salt, header_len).copy_characters_to_buffer(data->result, sizeof(data->result));
if (!fits) {
errno = EINVAL;
return nullptr;
}
data->result[header_len] = '$';
Crypto::Hash::SHA256 sha;
sha.update(StringView { key, strlen(key) });
sha.update(reinterpret_cast<u8 const*>(salt_value), salt_len);
auto digest = sha.digest();
auto string_or_error = encode_base64({ digest.immutable_data(), digest.data_length() });
if (string_or_error.is_error()) {
errno = ENOMEM;
return nullptr;
}
auto string = string_or_error.value().bytes_as_string_view();
fits = string.copy_characters_to_buffer(data->result + header_len + 1, sizeof(data->result) - header_len - 1);
if (!fits) {
errno = EINVAL;
return nullptr;
}
return data->result;
}
}