CID class
A Content Identifier (CID) for content-addressed data in IPFS.
CIDs are self-describing content addresses that combine a cryptographic hash of the content with metadata about the hashing algorithm and data encoding.
- CIDv0: legacy format, always SHA2-256 + DAG-PB, base58btc encoded.
- CIDv1: modern format with flexible codecs and multibase encoding.
Example:
final data = Uint8List.fromList(utf8.encode('Hello IPFS'));
final cid = await CID.fromContent(data);
print(cid.encode()); // bafkrei...
Constructors
- CID({required int version, required MultihashInfo multihash, String? codec, Multibase? multibaseType})
-
Creates a CID with the specified components.
const
- CID.v0(Uint8List hashBytes)
-
Creates a CIDv0 from a 32-byte SHA2-256 hash.
factory
- CID.v1(String codec, MultihashInfo multihash, {Multibase base = mb.Multibase.base32})
-
Creates a CIDv1 from a codec name and a multihash info.
factory
Properties
- codec → String?
-
The content codec (e.g., 'dag-pb', 'raw', 'dag-cbor').
final
- hashCode → int
-
The hash code for this object.
no setteroverride
- multibaseType → Multibase?
-
The multibase encoding type for string representation.
final
- multihash → MultihashInfo
-
The multihash containing the hash algorithm and digest.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- version → int
-
The CID version (0 or 1).
final
Methods
-
encode(
) → String - Encodes the CID to its string representation.
-
encodeWithBase(
Multibase? base) → String -
Encodes the CID using the requested
base. -
encodeWithBaseName(
String baseName) → String -
Encodes the CID using the base identified by
baseName. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toBytes(
) → Uint8List - Returns the raw binary representation of the CID.
-
toPrefixBytes(
) → Uint8List - Returns the CID prefix bytes (version + codec + multihash function + hash length), omitting the digest itself.
-
toString(
) → String -
Returns the encoded CID string.
override
-
validate(
) → bool - Validates the CID.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Static Methods
-
computeForData(
Uint8List data, {String format = 'raw'}) → Future< CID> -
Computes a CID for
data(async version for compatibility). -
computeForDataSync(
Uint8List data, {String codec = 'raw'}) → CID -
Computes a CID for
datasynchronously. -
decode(
String cidStr) → CID - Decodes a CID from its string representation.
-
fromBytes(
Uint8List bytes) → CID - Parses a CID from its raw binary representation.
-
fromContent(
Uint8List data, {String codec = 'raw', String hashType = 'sha2-256', int version = 1}) → Future< CID> -
Creates a CID by hashing
datawith the givenhashType. -
fromPrefixBytes(
Uint8List prefix, Uint8List data, {String hashType = 'sha2-256'}) → Future< CID> -
Reconstructs a CID from a
prefix(version + codec + multihash function