Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bindings/python/python/opendal/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
from datetime import datetime
from typing import Literal, TypeAlias, TypedDict

ChecksumAlgorithm: TypeAlias = Literal["crc32c", "crc64nvme", "md5", "sha1", "sha256"]
ChecksumAlgorithm: TypeAlias = Literal[
"crc32c", "crc64nvme", "crc64ecma", "md5", "sha1", "sha256"
]
"""Full-object checksum algorithms. Each service defines the algorithms it supports."""


Expand Down
1 change: 1 addition & 0 deletions bindings/python/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ pub(crate) fn parse_checksum_algorithm(name: &str) -> PyResult<ocore::ChecksumAl
match name {
"crc32c" => Ok(ocore::ChecksumAlgo::Crc32c),
"crc64nvme" => Ok(ocore::ChecksumAlgo::Crc64Nvme),
"crc64ecma" => Ok(ocore::ChecksumAlgo::Crc64Ecma),
"md5" => Ok(ocore::ChecksumAlgo::Md5),
"sha1" => Ok(ocore::ChecksumAlgo::Sha1),
"sha256" => Ok(ocore::ChecksumAlgo::Sha256),
Expand Down
1 change: 1 addition & 0 deletions bindings/python/tests/test_file_http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ async def test_write_and_open_forward_checksum(
[
("sha512", ValueError, "unknown checksum algorithm"),
(("crc32c", b"wrong"), TypeError, "checksum"),
("crc64ecma", opendal.exceptions.Unsupported, "full-object Crc64Ecma"),
("md5", opendal.exceptions.Unsupported, "full-object Md5"),
("sha1", opendal.exceptions.Unsupported, "full-object Sha1"),
("sha256", opendal.exceptions.Unsupported, "full-object Sha256"),
Expand Down
18 changes: 17 additions & 1 deletion core/core/src/types/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,26 @@ pub enum ChecksumAlgo {
Sha256,
/// SHA-1, a 20-byte digest.
Sha1,
/// CRC64-ECMA, the 8-byte digest used by Alibaba Cloud OSS and Tencent Cloud COS.
///
/// OSS and COS report unsigned decimal digests in `x-oss-hash-crc64ecma`
/// and `x-cos-hash-crc64ecma`, respectively.
///
/// CRC-64/XZ uses the canonical polynomial `0x42F0E1EBA9EA3693`, with
/// `refin = true`, `refout = true`, and `init` and `xorout` both set to
/// `0xFFFFFFFFFFFFFFFF`.
/// The check value for the ASCII bytes `123456789` is `0x995DC9BBDF1939FA`.
/// CRC-64/ECMA-182 (for example, `crc::CRC_64_ECMA_182`) yields
/// `0x6C40DF5F0B497347` for the same input and is incompatible with these
/// service digests.
Crc64Ecma,
}

impl ChecksumAlgo {
/// Return the length in bytes of a digest produced by this algorithm.
pub fn digest_len(self) -> usize {
match self {
ChecksumAlgo::Crc64Nvme => 8,
ChecksumAlgo::Crc64Nvme | ChecksumAlgo::Crc64Ecma => 8,
ChecksumAlgo::Crc32c => 4,
ChecksumAlgo::Md5 => 16,
ChecksumAlgo::Sha256 => 32,
Expand All @@ -53,6 +66,7 @@ impl ChecksumAlgo {
ChecksumAlgo::Md5 => 2,
ChecksumAlgo::Sha256 => 3,
ChecksumAlgo::Sha1 => 4,
ChecksumAlgo::Crc64Ecma => 5,
}
}

Expand All @@ -63,6 +77,7 @@ impl ChecksumAlgo {
2 => ChecksumAlgo::Md5,
3 => ChecksumAlgo::Sha256,
4 => ChecksumAlgo::Sha1,
5 => ChecksumAlgo::Crc64Ecma,
_ => unreachable!("compact values only store tags written by to_tag"),
}
}
Expand Down Expand Up @@ -222,6 +237,7 @@ mod tests {
(ChecksumAlgo::Md5, 2, 16),
(ChecksumAlgo::Sha256, 3, 32),
(ChecksumAlgo::Sha1, 4, 20),
(ChecksumAlgo::Crc64Ecma, 5, 8),
] {
assert_eq!(algo.to_tag(), tag);
assert_eq!(ChecksumAlgo::from_tag(tag), algo);
Expand Down
16 changes: 16 additions & 0 deletions core/layers/cache/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn checksum_algo_tag(algo: ChecksumAlgo) -> Option<u8> {
ChecksumAlgo::Crc32c => Some(2),
ChecksumAlgo::Md5 => Some(3),
ChecksumAlgo::Sha256 => Some(4),
ChecksumAlgo::Crc64Ecma => Some(5),
_ => None,
}
}
Expand All @@ -237,6 +238,7 @@ fn checksum_algo_from_tag(tag: u8) -> Option<ChecksumAlgo> {
2 => Some(ChecksumAlgo::Crc32c),
3 => Some(ChecksumAlgo::Md5),
4 => Some(ChecksumAlgo::Sha256),
5 => Some(ChecksumAlgo::Crc64Ecma),
_ => None,
}
}
Expand Down Expand Up @@ -340,6 +342,20 @@ mod tests {
assert!(Pointer::decode(Buffer::from(bad_checksum)).is_none());
}

#[test]
fn pointer_preserves_crc64_ecma_checksum() {
let checksum = Checksum::new(
ChecksumAlgo::Crc64Ecma,
0x995d_c9bb_df19_39fa_u64.to_be_bytes(),
);
let mut metadata = MetadataBuilder::file(9);
metadata.etag("\"abc\"").checksum(checksum.clone());
let pointer =
Pointer::from_metadata(metadata.build(), Timestamp::new(100, 200).unwrap()).unwrap();
let decoded = Pointer::decode(pointer.encode()).unwrap();
assert_eq!(decoded.metadata.checksum(), Some(checksum));
}

#[test]
fn pointer_v1_wire_format_is_stable_and_accepts_max_metadata() {
let mut metadata = MetadataBuilder::file(10);
Expand Down
1 change: 1 addition & 0 deletions core/layers/checksum/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl ReadVerifier {
fn new(expected: Checksum, remaining: Option<u64>) -> Option<Self> {
let hasher = match expected.algo {
ChecksumAlgo::Crc64Nvme => Hasher::Crc(CrcDigest::new(CrcAlgorithm::Crc64Nvme)),
ChecksumAlgo::Crc64Ecma => Hasher::Crc(CrcDigest::new(CrcAlgorithm::Crc64Xz)),
ChecksumAlgo::Crc32c => Hasher::Crc(CrcDigest::new(CrcAlgorithm::Crc32Iscsi)),
ChecksumAlgo::Md5 => Hasher::Md5(Md5::new()),
ChecksumAlgo::Sha256 => Hasher::Sha256(Sha256::new()),
Expand Down
17 changes: 17 additions & 0 deletions core/layers/checksum/tests/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ async fn verifies_unsolicited_digests_with_no_read_capability() -> Result<()> {
// Standard check values for the same byte sequence, in wire digest order.
for (algo, expected) in [
(ChecksumAlgo::Crc64Nvme, "ae8b14860a799888"),
(ChecksumAlgo::Crc64Ecma, "995dc9bbdf1939fa"),
(ChecksumAlgo::Crc32c, "e3069283"),
(ChecksumAlgo::Md5, "25f9e794323b453885f5181f1b624d0b"),
(
Expand Down Expand Up @@ -529,6 +530,22 @@ async fn verifies_unsolicited_digests_with_no_read_capability() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn rejects_ecma_182_digest_for_vendor_crc64_ecma() -> Result<()> {
let fixture = Fixture {
checksum: Some(Checksum::new(
ChecksumAlgo::Crc64Ecma,
0x6c40_df5f_0b49_7347_u64.to_be_bytes(),
)),
..Fixture::new(GOOD)
};
let op = fixture
.operator()?
.layer(ChecksumLayer::new().require_read(true));
assert_mismatch(op.read("object").await.unwrap_err());
Ok(())
}

#[tokio::test]
async fn checks_reported_length_and_finalizes_at_eof_without_a_positive_length() -> Result<()> {
for (payload, length) in [
Expand Down
13 changes: 13 additions & 0 deletions core/services/cos/src/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ Depending on its configuration and the backing system, this service can expose:
Inspect the effective capability set with [`opendal_core::Operator::info`] and
[`opendal_core::OperatorInfo::capability`] after building an operator.

## Checksum format

Tencent Cloud COS reports an unsigned decimal checksum named CRC64-ECMA in
`x-cos-hash-crc64ecma`. Alibaba Cloud OSS uses the same checksum in
`x-oss-hash-crc64ecma`. OpenDAL represents it as `ChecksumAlgo::Crc64Ecma`.

CRC-64/XZ uses the canonical polynomial `0x42F0E1EBA9EA3693`, with
`refin = true`, `refout = true`, and `init` and `xorout` both set to
`0xFFFFFFFFFFFFFFFF`. The check value for the ASCII bytes `123456789` is
`0x995DC9BBDF1939FA`. CRC-64/ECMA-182 (for example, `crc::CRC_64_ECMA_182`)
yields `0x6C40DF5F0B497347` for the same input and is incompatible with
these service digests.

## Configuration

Use [`crate::CosConfig`] for serializable configuration and this builder's
Expand Down
13 changes: 13 additions & 0 deletions core/services/oss/src/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ For signed full-object reads and stat requests, OSS can override the response
`Cache-Control` and `Content-Disposition` headers. These options also apply to
presigned requests and leave the stored object metadata unchanged.

## Checksum format

Alibaba Cloud OSS reports an unsigned decimal checksum named CRC64-ECMA in
`x-oss-hash-crc64ecma`. Tencent Cloud COS uses the same checksum in
`x-cos-hash-crc64ecma`. OpenDAL represents it as `ChecksumAlgo::Crc64Ecma`.

CRC-64/XZ uses the canonical polynomial `0x42F0E1EBA9EA3693`, with
`refin = true`, `refout = true`, and `init` and `xorout` both set to
`0xFFFFFFFFFFFFFFFF`. The check value for the ASCII bytes `123456789` is
`0x995DC9BBDF1939FA`. CRC-64/ECMA-182 (for example, `crc::CRC_64_ECMA_182`)
yields `0x6C40DF5F0B497347` for the same input and is incompatible with
these service digests.

## Configuration

Use [`crate::OssConfig`] for serializable configuration and this builder's
Expand Down
2 changes: 1 addition & 1 deletion dev/src/generate/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn generate_options_py(workspace_dir: &Path) -> Result<()> {
"\nfrom __future__ import annotations\n\nfrom datetime import datetime\nfrom typing import Literal, TypeAlias, TypedDict\n",
);
out.push_str(
"\nChecksumAlgorithm: TypeAlias = Literal[\"crc32c\", \"crc64nvme\", \"md5\", \"sha1\", \"sha256\"]\n\"\"\"Full-object checksum algorithms. Each service defines the algorithms it supports.\"\"\"\n",
"\nChecksumAlgorithm: TypeAlias = Literal[\n \"crc32c\", \"crc64nvme\", \"crc64ecma\", \"md5\", \"sha1\", \"sha256\"\n]\n\"\"\"Full-object checksum algorithms. Each service defines the algorithms it supports.\"\"\"\n",
);

let mut exports = vec!["ChecksumAlgorithm".to_string()];
Expand Down
Loading