Skip to content

Commit

Permalink
Added more storage examples using filsystem for go and android
Browse files Browse the repository at this point in the history
  • Loading branch information
daeMOn63 committed Jan 31, 2020
1 parent a5fd526 commit 8238455
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
69 changes: 67 additions & 2 deletions README.md
Expand Up @@ -217,15 +217,80 @@ val cfg = SymNameAndPassword()
cfg.name = "deviceXYZ"
cfg.password = "secretForDeviceXYZ"

val state = ByteArray(0)
val client = E4.newClient(cfg, E4.newMemoryStore(state))
val store = FileStore(File(filesDir.absolutePath + "/" + cfg.name + ".json"))
val client = E4.newClient(cfg, store)

// From here, messages can be protected / unprotected :
val topic = "/deviceXYZ/data";
val protectedMessage = client.protectMessage("Hello".toByteArray(Charsets.UTF_8), topic)
val unprotectedMessage = client.unprotect(protectedMessage, topic)
```

Here We are using a custom file storage implemented as such:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import io.teserakt.e4.Store;

public class FileStore implements Store {
private static final int SEEK_START = 0;
private static final int SEEK_CURRENT = 1;
private static final int SEEK_END = 2;

private File file;
private int index;

public FileStore(File file) {
this.file = file;
this.index = 0;
}

public long read(byte[] buf) throws Exception {
FileInputStream fis = new FileInputStream(this.file);
int n = fis.read(buf, this.index, buf.length);
fis.close();

this.index += n;
return n;
}

public long write(byte[] buf) throws Exception {
FileOutputStream fos = new FileOutputStream(this.file);
fos.write(buf, this.index, buf.length);
fos.flush();
fos.close();

this.index += buf.length;
return buf.length;
}

public long seek(long offset, long whence) throws Exception {
long abs;
switch((int)whence) {
case SEEK_START:
abs = offset;
break;
case SEEK_CURRENT:
abs = this.index + offset;
break;
case SEEK_END:
abs = this.file.length() + offset;
break;
default:
throw new Exception("invalid whence");
}
if (abs < 0) {
throw new Exception("negative position");
}

this.index = (int)abs;
return abs;
}
}
```

## Contributing

Before contributing, please read our [CONTRIBUTING](./CONTRIBUTING.md) guide.
Expand Down
21 changes: 21 additions & 0 deletions example_test.go
Expand Up @@ -16,6 +16,7 @@ package e4_test

import (
"fmt"
"os"

e4 "github.com/teserakt-io/e4go"
e4crypto "github.com/teserakt-io/e4go/crypto"
Expand All @@ -36,6 +37,26 @@ func ExampleNewClient_symIDAndKey() {
fmt.Printf("Protected message: %v", protectedMessage)
}

func ExampleNewClient_fileStorage() {
f, err := os.OpenFile("/storage/clientID.json", os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
panic(err)
}
defer f.Close()

client, err := e4.NewClient(&e4.SymIDAndKey{ID: []byte("clientID"), Key: e4crypto.RandomKey()}, f)
if err != nil {
panic(err)
}

protectedMessage, err := client.ProtectMessage([]byte("very secret message"), "topic/name")
if err != nil {
panic(err)
}

fmt.Printf("Protected message: %v", protectedMessage)
}

func ExampleNewClient_symNameAndPassword() {
client, err := e4.NewClient(&e4.SymNameAndPassword{Name: "clientName", Password: "verySecretPassword"}, e4.NewMemoryStore(nil))
if err != nil {
Expand Down

0 comments on commit 8238455

Please sign in to comment.