ComputerCraft Forums

ComputerCraft => Programs => APIs and Utilities => Topic started by: KingofGamesYami on Aug 13, 2018, 02:07 AM

Title: Blink - Communication API with Sender Verification
Post by: KingofGamesYami on Aug 13, 2018, 02:07 AM
Reposted from here (http://www.computercraft.info/forums2/index.php?/topic/27228-blink-communication-api-with-sender-verification/)

Blink is essentially a replacement for rednet, adding a way to verify the identity of the sender. This does not encrypt or otherwise secure your messages!
Installation
pastebin get YGTnaHtd blink (http://pastebin.com/YGTnaHtd)
Usage
Setup
This doesn't have to happen every time you run the program, your choices are automatically saved to a file
Before transmitting messages, you must tell the sender what it's secret key is. This is done using setDeviceKey.
blink.setDeviceKey( "MySecretPassword" )
After this step, you must tell the receiver what that key was. This is done using pair.
blink.pair( SENDER_ID, "MySecretPassword" )
SENDER_ID is the id of the sending computer with that secret key. This can be obtained by running "id" in the shell of that computer.
Starting Up
While your computer is sending or receiving messages, blink.run must be running in parallel with your program. This would look something like this:
parallel.waitForAny( blink.run, function()
-- your program here
end )
If you wish to use an alternative means of running blink.run, such as multishell or statemachine (https://forums.computercraft.cc/index.php?topic=33.0), you are welcome to do so. However, this is by far the simplest way to use it.
Sending and Receiving Messages
The receiving computer must be paired and blink.run must be appropriately integrated
To send a message, you need to know the ID of the computer you wish to contact in addition to your message contents. Optionally, you can specify a timeout, because the function will hang indefinitely if the receiver is not available.
local success = blink.send( 3, "Hello, World!", 1 ) --sends "Hello, World!" to computer 3 (times out after 1 second)
To receive a message, you can either pull a "blink_message" event or use blink.receive.
local id, message = blink.receive( SENDER_ID )
SENDER_ID must be the id you wish to receive from. The function need not return id, but I do so to retain some similarity with recnet.receive.
local event, message, from = os.pullEvent( "blink_message" )
Really not anything to explain here, if you're at all familiar with the computercraft event system.
Features
Credit
Anavrins (http://www.computercraft.info/forums2/index.php?/user/12870-anavrins/), for his sha256 API (downloaded as "hash" automatically).
Alex Kloss for the implementation of base64 I'm using
*Credits are given in more detail in the code itself.