使函数使用 std::span 而不是旧方法
Making a function use std::span instead of the old way
我刚刚了解到我可以将 VS 2019 与 C++ 20 一起使用,我正在尝试使用它。我正在尝试使以下函数使用 std::span
,因为这样 data_size
和 key_size
将是多余的。
除了 *data
和 data++
之外,一切都很好。它应该是什么?
int rc4(rc4_context* context, const std::uint8_t* data, const std::size_t data_size, const std::uint8_t* key, const std::size_t key_size, std::uint8_t* output)
{
std::uint32_t i, j;
// Check parameters
if (!context || !key)
return ERROR_INVALID_PARAMETER;
// Clear context
context->i = 0;
context->j = 0;
// Initialize the S array with identity permutation
for (i = 0; i < 256; i++)
{
context->s[i] = static_cast<std::uint8_t>(i);
}
// S is then processed for 256 iterations
for (i = 0, j = 0; i < 256; i++)
{
// Randomize the permutations using the supplied key
j = (j + context->s[i] + key[i % key_size]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = context->s[i];
context->s[i] = context->s[j];
context->s[j] = temp;
}
// Restore context
i = context->i;
j = context->j;
auto* s = context->s;
// Encryption loop
for (size_t x = 0; x < data_size; ++x)
{
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = s[i];
s[i] = s[j];
s[j] = temp;
// Valid input and output?
if (data && output)
{
// XOR the input data with the RC4 stream
*output = *data ^ s[(s[i] + s[j]) % 256];
// Increment data pointers
data++;
output++;
}
}
// Save context
context->i = i;
context->j = j;
return NO_ERROR;
}
我的尝试
int rc4(rc4_context* context, const std::span<uint8_t*> data, const std::span<std::uint8_t*> key, std::uint8_t* output)
{
// INITIALIZATION
std::uint32_t i, j;
// Check parameters
if (!context || !key.empty())
return ERROR_INVALID_PARAMETER;
// Clear context
context->i = 0;
context->j = 0;
// Initialize the S array with identity permutation
for (i = 0; i < 256; i++)
{
context->s[i] = static_cast<std::uint8_t>(i);
}
// S is then processed for 256 iterations
for (i = 0, j = 0; i < 256; i++)
{
// Randomize the permutations using the supplied key
j = (j + context->s[i] + key[i % key.size()]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = context->s[i];
context->s[i] = context->s[j];
context->s[j] = temp;
}
// MAIN LOGIC PART
// Restore context
i = context->i;
j = context->j;
auto* s = context->s;
// Encryption loop
for (size_t x = 0; x < data.size(); ++x)
{
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = s[i];
s[i] = s[j];
s[j] = temp;
// Valid input and output?
if (data.empty() && output)
{
// XOR the input data with the RC4 stream
*output = *data ^ s[(s[i] + s[j]) % 256];
// Increment data pointers
data++;
output++;
}
}
// Save context
context->i = i;
context->j = j;
return NO_ERROR;
}
首先,(const std::uint8_t* data, const std::size_t data_size)
这对参数可以用std::span<uint8_t>
代替,而不是std::span<uint8_t*>
。
其次,您无需费心递增 data
,因为您可以将其重写为基于范围的 for 循环:
for (uint8_t elem : data) {
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
std::swap(s[i], s[j]);
// Valid output?
if (output) {
// XOR the input data with the RC4 stream
*output++ = elem ^ s[(s[i] + s[j]) % 256];
}
}
我刚刚了解到我可以将 VS 2019 与 C++ 20 一起使用,我正在尝试使用它。我正在尝试使以下函数使用 std::span
,因为这样 data_size
和 key_size
将是多余的。
除了 *data
和 data++
之外,一切都很好。它应该是什么?
int rc4(rc4_context* context, const std::uint8_t* data, const std::size_t data_size, const std::uint8_t* key, const std::size_t key_size, std::uint8_t* output)
{
std::uint32_t i, j;
// Check parameters
if (!context || !key)
return ERROR_INVALID_PARAMETER;
// Clear context
context->i = 0;
context->j = 0;
// Initialize the S array with identity permutation
for (i = 0; i < 256; i++)
{
context->s[i] = static_cast<std::uint8_t>(i);
}
// S is then processed for 256 iterations
for (i = 0, j = 0; i < 256; i++)
{
// Randomize the permutations using the supplied key
j = (j + context->s[i] + key[i % key_size]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = context->s[i];
context->s[i] = context->s[j];
context->s[j] = temp;
}
// Restore context
i = context->i;
j = context->j;
auto* s = context->s;
// Encryption loop
for (size_t x = 0; x < data_size; ++x)
{
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = s[i];
s[i] = s[j];
s[j] = temp;
// Valid input and output?
if (data && output)
{
// XOR the input data with the RC4 stream
*output = *data ^ s[(s[i] + s[j]) % 256];
// Increment data pointers
data++;
output++;
}
}
// Save context
context->i = i;
context->j = j;
return NO_ERROR;
}
我的尝试
int rc4(rc4_context* context, const std::span<uint8_t*> data, const std::span<std::uint8_t*> key, std::uint8_t* output)
{
// INITIALIZATION
std::uint32_t i, j;
// Check parameters
if (!context || !key.empty())
return ERROR_INVALID_PARAMETER;
// Clear context
context->i = 0;
context->j = 0;
// Initialize the S array with identity permutation
for (i = 0; i < 256; i++)
{
context->s[i] = static_cast<std::uint8_t>(i);
}
// S is then processed for 256 iterations
for (i = 0, j = 0; i < 256; i++)
{
// Randomize the permutations using the supplied key
j = (j + context->s[i] + key[i % key.size()]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = context->s[i];
context->s[i] = context->s[j];
context->s[j] = temp;
}
// MAIN LOGIC PART
// Restore context
i = context->i;
j = context->j;
auto* s = context->s;
// Encryption loop
for (size_t x = 0; x < data.size(); ++x)
{
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
const auto temp = s[i];
s[i] = s[j];
s[j] = temp;
// Valid input and output?
if (data.empty() && output)
{
// XOR the input data with the RC4 stream
*output = *data ^ s[(s[i] + s[j]) % 256];
// Increment data pointers
data++;
output++;
}
}
// Save context
context->i = i;
context->j = j;
return NO_ERROR;
}
首先,(const std::uint8_t* data, const std::size_t data_size)
这对参数可以用std::span<uint8_t>
代替,而不是std::span<uint8_t*>
。
其次,您无需费心递增 data
,因为您可以将其重写为基于范围的 for 循环:
for (uint8_t elem : data) {
// Adjust indices
i = (i + 1) % 256;
j = (j + s[i]) % 256;
// Swap the values of S[i] and S[j]
std::swap(s[i], s[j]);
// Valid output?
if (output) {
// XOR the input data with the RC4 stream
*output++ = elem ^ s[(s[i] + s[j]) % 256];
}
}