Giter Club home page Giter Club logo

tisodium's Introduction

TiSodium Appcelerator Titanium

Titanium Mobile binding to sodium crypto library based on sodium-jni. It implements XSalsa20 encryption.

Example usage

sodium = require 'org.farwayer.ti.sodium'


toBuffer: (blob) ->
  buffer = Ti.createBuffer(length: blob.length)
  stream = Ti.Stream.createStream(source: blob, mode: Ti.Stream.MODE_READ)
  stream.read(buffer)
  return buffer


# generate new cipher key
# you must save it in safe place
key = sodium.newKey()

# create new cipher
cipher = sodium.createCipher(key: key)


encrypt: (image, path, success) ->
  # convert Ti.Blob -> Ti.Buffer
  data = toBuffer(image)

  cipher.encrypt
    data: data
    success: (e) ->
      # unique random encryption 'nonce' for current data
      # you must save it anywhere for latter decryption
      # we will save it in the same file
      # but nonce can be saved separately from encrypted data
      # for more security
      nonce = e.nonce

      # ciphertext is Ti.Buffer
      encryptedData = e.ciphertext

      file = Ti.Filesystem.getFile(path)
      stream = file.open(Ti.Filesystem.MODE_WRITE)
      stream.write(nonce)
      stream.write(encryptedData)
      stream.close()

      success()


decrypt: (path, success) ->
  file = Ti.Filesystem.getFile(path)
  stream = file.open(Ti.Filesystem.MODE_READ)

  nonceSize = sodium.NONCE_SIZE
  nonce = Ti.createBuffer(length: nonceSize)
  stream.read(nonce)

  dataSize = file.size - nonceSize
  ciphertext = Ti.createBuffer(length: dataSize)
  stream.read(ciphertext)

  stream.close()

  @cipher.decrypt
    ciphertext: ciphertext
    nonce: nonce
    success: (e) ->
      # e.data is Ti.Buffer
      # convert it to Ti.Blob
      image = e.data.toBlob()
      success(image)


path = "#{Ti.Filesystem.externalStorageDirectory}/encrypted.raw"

Ti.Media.showCamera
  success: (e) ->
    image = e.media

    encrypt image, path, ->
      # encrypted and saved to path, try to decrypt

      decrypt path, (decryptedImage) ->
        # decrypted

tisodium's People

Contributors

farwayer avatar

Stargazers

Nazır Doğan avatar Mahmoud Kamal avatar Dan Tamas avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.