CID class
A Content Identifier (CID) for content-addressed data in IPFS.
CIDs are self-describing content addresses used to uniquely identify data in IPFS and other distributed systems. They combine a cryptographic hash of the content with metadata about the hashing algorithm and data encoding format.
IPFS supports two CID versions:
- CIDv0: Legacy format, always SHA2-256 + DAG-PB, base58btc encoded
- CIDv1: Modern format with flexible codecs and multibase encoding
Example:
// Create CID from content
final data = Uint8List.fromList(utf8.encode('Hello IPFS'));
final cid = await CID.fromContent(data);
// print('CID: ${cid.encode()}'); // bafkreif...
// Decode existing CID
final decoded = CID.decode('QmYwAPJzv5CZsnA...');
// print('Version: ${decoded.version}');
See also:
- IPFS CID Specification
Blockfor content-addressed data storage
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.
CIDv0 is always: SHA2-256, DAG-PB, Base58BTC.
factory
- CID.v1(String codec, MultihashInfo multihash, {Multibase base = Multibase.base32})
-
Creates a CIDv1.
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.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toBytes(
) → Uint8List - Converts the CID to its binary representation.
-
toProto(
) → IPFSCIDProto - Converts the CID to a Protobuf representation.
-
toString(
) → String -
A string representation of this object.
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 CID for data (async version for compatibility).
-
computeForDataSync(
Uint8List data, {String codec = 'raw'}) → CID - Computes CID for data (sync version).
-
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 content, {String codec = 'raw', String hashType = 'sha2-256', int version = 1}) → Future< CID> - Creates a CID from raw content.
-
fromProto(
IPFSCIDProto proto) → CID - Creates a CID from a Protobuf representation.