Skip to content

Latest commit

 

History

History
135 lines (104 loc) · 4.44 KB

README.md

File metadata and controls

135 lines (104 loc) · 4.44 KB

Super lightweight encryption lib

FFP cypher is totally insecure and naive, use for educational purposes only 💖 image

index

  • RSA (for asymmetric encryption)
  • XOR (for symmetric encryption)
  • hacha (simple SDBM hash function with reasonable entropy)
  • requiresWork (string, difficulty)
  • proofOfWork (string, hash, nonce)

usage

  • asymmetric encryption
  //
  // one step
  const rsa = require('../src/RSAKeys');
  const keys = rsa.generateKeys(512);
  //Generated Random public key,
  //jb1yruw21v4t0am0tr0msy5iam07oawertgg6joo48vtm30hanf1fabmjnlxnvupcene6qyg836512blu
  //cnacq9dja0hght517y5n90iag8izipy1w8hf4ot62bos6vn9o5nn1ygmqgjhuss3xqp9w0b86alrks2c9
  //5xu9s9ihqf38atrd5r0b6szknf3eronmno1d/qjbiu8xs0msnk88sfh4dhb7va71p7xssw2u4e9b8ijqoh3avh
  //-------------------------------------------
  //Generated private key,
  //ha3mn77xg0dzm1g53xtirzwlnr2kjgrdjcpqlk1dley8s2ulwp6tyd0eryvn9m619eth4fjygntg4cu7ju
  //7fi3c5ek1grizehjyxplq4ui1mk8pykegf7fx4nojeay9nvrhjvamepw1bzt88durduvmvr5500qaxv6s
  //znx4wpzm7uroe0r6qzq44e4djnudjwh8xpt
  //-------------------------------------------


  // encryption
  const RSA = require('../src/RSA').RSA;
  const $rsa = new RSA(keys.bits);

  const cypher = $rsa.encrypt("olivier!",keys.publicKey);
  const message = $rsa.decrypt(cypher,keys.publicKey,keys.privateKey);
  • symmetric encryption
  const XOR = require('../src/XOR');
  const xor = new XOR();
  const cypher = xor.encrypt("olivier!",'privatekey');
  const message = xor.decrypt(cypher,'privatekey');
  • proof of work
  const requiresWork = require('../src/utils').requiresWork;
  const proofOfWork = require('../src/utils').proofOfWork;

    const string = 'Olivier is learning something here';
    //
    // CPU difficulty lower => 0x4fffn or higher => 0x8fffn, or 0xffffn 
    const difficulty = 0x6fffn; // ~400ms on my computer
    const work = requiresWork(string,difficulty);
    
    //
    // verify proof
    proofOfWork(string,work[0],work[1]).should.equal(true);
  • hacha
  const hacha = require('../src/utils').hacha;
  //
  // convert string to 128bits BigInt with reasonable entropy and poor collision
  const hex = hacha('oliviertest');
  hex.toString(16).should.equal('57a8eb282a383a5ec');

Documentation

Entropy

Math behind Hash functions

relatively Prime

  • e is relatively Prime of a if PGCD(e,a) = 1

modular inverse

If a aa and NNN are integers such that gcd⁡(a,N)=1, then there exists an integer x xx such that ax≡1(modN).

  • x≡a⁻¹ (mod N)
  • (a * X) % N = 1

Modulo operation

Modular exponentiation

Our HaHaCha

image

Simple RSA (asymmetric)

XOR cypher (symmetric)

theorem

Let m be a plaintext message, k be the encryption key, and c be the encrypted message.

Notes