<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ocr.michiyo.me/w/index.php?action=history&amp;feed=atom&amp;title=Component%2Fdata</id>
	<title>Component/data - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://ocr.michiyo.me/w/index.php?action=history&amp;feed=atom&amp;title=Component%2Fdata"/>
	<link rel="alternate" type="text/html" href="https://ocr.michiyo.me/w/index.php?title=Component/data&amp;action=history"/>
	<updated>2026-08-24T13:45:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://ocr.michiyo.me/w/index.php?title=Component/data&amp;diff=1148&amp;oldid=prev</id>
		<title>imported&gt;OCDoc Import: Imported from legacy OpenComputers documentation at ocdoc.cil.li</title>
		<link rel="alternate" type="text/html" href="https://ocr.michiyo.me/w/index.php?title=Component/data&amp;diff=1148&amp;oldid=prev"/>
		<updated>2026-08-24T08:08:27Z</updated>

		<summary type="html">&lt;p&gt;Imported from legacy OpenComputers documentation at ocdoc.cil.li&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Component: Data =&lt;br /&gt;
&lt;br /&gt;
This component is provided by the [[Item/data card|Data Card]]&lt;br /&gt;
&lt;br /&gt;
Component name: `data`.&lt;br /&gt;
&lt;br /&gt;
== Tier 1 Callbacks ==&lt;br /&gt;
&lt;br /&gt;
* `crc32(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Computes CRC-32 hash of the data. Result is in binary format.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `decode64(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Applies base64 decoding to the data.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `encode64(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Applies base64 encoding to the data. Result is in binary format.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `md5(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Computes MD5 hash of the data. Result is in binary format&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `sha256(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Computes SHA2-256 hash of the data. Result is in binary format.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `deflate(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Applies deflate compression to the data.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `inflate(data:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Applies inflate decompression to the data.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `getLimit():number`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The maximum size of data that can be passed to other functions of the card.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Tier 2 Callbacks ==&lt;br /&gt;
&lt;br /&gt;
* `encrypt(data:string, key:string, iv:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Applies AES encryption to the data using the key and (preferably) random IV.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `decrypt(data:string, key:string, iv:string):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Reverses AES encryption on the data using the key and the IV.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `random(len:number):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Generates a random binary string of `len` length.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Tier 3 Callbacks ==&lt;br /&gt;
&lt;br /&gt;
* `generateKeyPair([bitLen:number]):table, table`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Generates a public/private key pair for various cryptiographic functions.  &lt;br /&gt;
Optional second parameter specifies key length, 256 or 384 bits accepted.  &lt;br /&gt;
Key types include &amp;quot;ec-public&amp;quot; and &amp;quot;ec-private&amp;quot;. Keys can be serialized with  &lt;br /&gt;
`key.serialize():string` Keys also contain the function `key.isPublic():boolean`&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `ecdsa(data:string, key:userdata[, sig:string]):string or boolean`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Generates a signiture of data using a private key. If signature is present  &lt;br /&gt;
verifies the signature using the public key, the previously generated  &lt;br /&gt;
signature string and the original string.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `ecdh(privateKey:userdata, publicKey:userdata):string`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Generates a Diffie-Hellman shared key using the first user&amp;#039;s private key and  &lt;br /&gt;
the second user&amp;#039;s public key. An example of a basic key relation:  &lt;br /&gt;
`ecdh(userA.private, userB.public) == ecdh(userB.private, userA.public)`&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* `deserializeKey(data:string, type:string):table`&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Transforms a key from string to it&amp;#039;s arbitrary type.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
This card can be used to transmit encrypted data to other in-game or real-life peers. Since we are given the ability to create key-pairs and Diffie-Hellman shared keys, we are able to establish encrypted connections with these peers.&lt;br /&gt;
&lt;br /&gt;
When using key pairs for encryption, the basic concept is this&lt;br /&gt;
&lt;br /&gt;
Preliminary Setup:&lt;br /&gt;
&lt;br /&gt;
* (The following items are to be done on the RECEIVER)&lt;br /&gt;
* Generate a public key (rPublic) and private key (rPrivate).&lt;br /&gt;
&amp;#039;&amp;#039; &amp;#039;&amp;#039;\*If no automated key exchange, then you&amp;#039;ll need to send rPublic to the SENDER manually.&lt;br /&gt;
&lt;br /&gt;
The SENDER must:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039; &amp;#039;&amp;#039;\&amp;#039;&amp;#039;\&amp;#039;&amp;#039;Read the RECEIVER&amp;#039;s public key (rPublic), unserialize it, and rebuild the key object.&lt;br /&gt;
* Generate a public key (sPublic) and private key (sPrivate).&lt;br /&gt;
&amp;#039;&amp;#039; &amp;#039;&amp;#039;Generate an encryption key using rPublic and sPrivate.&lt;br /&gt;
* Generate an Initialization Vector (IV).&lt;br /&gt;
* Convert sPublic into a string with sPublic.serialize().&lt;br /&gt;
&amp;#039;&amp;#039; &amp;#039;&amp;#039;\&amp;#039;&amp;#039;\&amp;#039;&amp;#039;Serialize the data using the serialization library, then encrypt it using the encryption key and IV.&lt;br /&gt;
* Serialize and transmit the message, with sPublic and IV in plain-text.&lt;br /&gt;
&lt;br /&gt;
The RECEIVER must:&lt;br /&gt;
&lt;br /&gt;
* Read the RECEIVER&amp;#039;s private key (rPrivate), unserialize it, and rebuild the key object.&lt;br /&gt;
* Receive the message and unserialize it using the serialization library, then deserialize sPublic using data.deserializeKey().&lt;br /&gt;
&amp;#039;&amp;#039; &amp;#039;&amp;#039;Generate a decryption key using sPublic and rPrivate.&lt;br /&gt;
* Use the decryption key, along with the IV, to decrypt the message.&lt;br /&gt;
* Unserialize the decrypted data.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;NOTE&amp;#039;&amp;#039;&amp;#039;* In the above, the terms &amp;#039;encryption key&amp;#039; and &amp;#039;decryption key&amp;#039; are used. These keys are, byte-for-byte, the same. This is because both keys were generated using the `ecdh()` function.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;NOTE&amp;#039;&amp;#039;&amp;#039;\** In the above, it is stated that &amp;#039;&amp;#039;you will manually transfer rPublic to SENDER&amp;#039;&amp;#039;. This would not be the case in systems that employ a handshake protocol. For example, SENDER would make themselves known to RECEIVER, who will then reply to SENDER with a public key (and possibly additional information, such as key-length). For simplicity, the following examples will not cover the functions of handshake protocols.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;NOTE&amp;#039;&amp;#039;&amp;#039;*** The examples above and below state that you must serialize/unserialize a key or message. In-general, it is good practice to serialize data (especially when in binary format) before you write it to a file, or transfer it on the network. Serialization makes sure that the binary data is &amp;#039;escaped&amp;#039;, making it safe for your script or shell to read.&lt;br /&gt;
&lt;br /&gt;
To send an encrypted message: ```lua local serialization = require(&amp;quot;serialization&amp;quot;) local component = require(&amp;quot;component&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- This table contains the data that will be sent to the receiving computer. -- Along with header information the receiver will use to decrypt the message. local __packet = {&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;  header =&lt;br /&gt;
  {&lt;br /&gt;
      sPublic    = nil,&lt;br /&gt;
      iv         = nil&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  data = nil&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- Read the public key file. local file = io.open(&amp;quot;rPublic&amp;quot;,&amp;quot;rb&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local rPublic = file:read(&amp;quot;*a&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
file:close()&lt;br /&gt;
&lt;br /&gt;
-- Unserialize the public key into binary form. local rPublic = serialization.unserialize(rPublic)&lt;br /&gt;
&lt;br /&gt;
-- Rebuild the public key object. local rPublic = component.data.deserializeKey(rPublic,&amp;quot;ec-public&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Generate a public and private keypair for this session. local sPublic, sPrivate = component.data.generateKeyPair(384)&lt;br /&gt;
&lt;br /&gt;
-- Generate an encryption key. local encryptionKey = component.data.md5(component.data.ecdh(sPrivate, rPublic))&lt;br /&gt;
&lt;br /&gt;
-- Set the header value &amp;#039;iv&amp;#039; to a randomly generated 16 digit string. __packet.header.iv = component.data.random(16)&lt;br /&gt;
&lt;br /&gt;
-- Set the header value &amp;#039;sPublic&amp;#039; to a string. __packet.header.sPublic = sPublic.serialize()&lt;br /&gt;
&lt;br /&gt;
-- The data that is to be encrypted. __packet.data = &amp;quot;lorem ipsum&amp;quot;&lt;br /&gt;
&lt;br /&gt;
-- Data is serialized and encrypted. &amp;lt;span class=&amp;quot;underline&amp;quot;&amp;gt;packet.data = component.data.encrypt(serialization.serialize(&amp;lt;/span&amp;gt;packet.data), encryptionKey, __packet.header.iv)&lt;br /&gt;
&lt;br /&gt;
-- For simplicity, in this example the computers are using a Linked Card (ocdoc.cil.li/item:linked_card) component.tunnel.send(serialization.serialize(__packet)) ``` To receive the encrypted message: ```lua local serialization = require(&amp;quot;serialization&amp;quot;) local component = require(&amp;quot;component&amp;quot;) local event = require(&amp;quot;event&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Read the private key local file = io.open(&amp;quot;rPrivate&amp;quot;,&amp;quot;rb&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local rPrivate = file:read(&amp;quot;*a&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
file:close()&lt;br /&gt;
&lt;br /&gt;
-- Unserialize the private key local rPrivate = serialization.unserialize(rPrivate)&lt;br /&gt;
&lt;br /&gt;
-- Rebuild the private key object local rPrivate = component.data.deserializeKey(rPrivate,&amp;quot;ec-private&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Use event.pull() to receive the message from SENDER. local _, _, _, _, _, message = event.pull(&amp;quot;modem_message&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Unserialize the message local message = serialization.unserialize(message)&lt;br /&gt;
&lt;br /&gt;
-- From the message, deserialize the public key. local sPublic = component.data.deserializeKey(message.header.sPublic,&amp;quot;ec-public&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
-- Generate the decryption key. local decryptionKey = component.data.md5(component.data.ecdh(rPrivate, sPublic))&lt;br /&gt;
&lt;br /&gt;
-- Use the decryption key and the IV to decrypt the encrypted data in message.data local data = component.data.decrypt(message.data, decryptionKey, message.header.iv)&lt;br /&gt;
&lt;br /&gt;
-- Unserialize the decrypted data. local data = serialization.unserialize(data)&lt;br /&gt;
&lt;br /&gt;
== -- Print the decrypted data. print(data) ``` ==&lt;br /&gt;
&lt;br /&gt;
{{:Component/contents}}&lt;br /&gt;
&lt;/div&gt;</summary>
		<author><name>imported&gt;OCDoc Import</name></author>
	</entry>
</feed>