Git Product home page Git Product logo

ascon-js's Introduction

Ascon JS

TypeScript implementation of Ascon v1.2, an authenticated cipher and hash function http://ascon.iaik.tugraz.at/.

This implementation is a port of the Python version.

npm npm NPM

Usage

This library can be used both in the browser and in NodeJS.

Import the Ascon library as follows:

import { Ascon, randomBytes } from "ascon-js";

Hash

const message = new TextEncoder().encode("ascon");

const hash = Ascon.hash(message); // 32 byte hash

All four algorithms can be used. Fixed hash length of 32 bytes:

  • Ascon-Hash (default)
  • Ascon-Hasha

Variable hash length (should be >= 32 bytes):

  • Ascon-Xof
  • Ascon-Xofa
Ascon.hash(message, {
  variant: "Ascon-Xof",
  length: 64,
});

Encryption

// Generate a random key and a random nonce. This can be done using the helper method randomBytes
const key = randomBytes(16); // 16 bytes
const nonce = randomBytes(16); // 16 bytes

const plaintext = new TextEncoder().encode("ascon");

// Encrypt the plaintext using the key and the nonce
const ciphertext = Ascon.encrypt(key, nonce, plaintext);

Associated data

In addition to the plain text, associated data can be provided, which is used for additional data integrity checking.

Ascon.encrypt(key, nonce, plaintext, {
  associatedData: new TextEncoder().encode("additional data"),
});

Algorithms

All specified algorithms of Ascon encryption are implemented:

  • Ascon-128 (default)
  • Ascon-128a
  • Ascon-80pq

The used algorithm can be specified using the options parameter.

const key = randomBytes(20); // 20 bytes

Ascon.encrypt(key, nonce, plaintext, {
  variant: "Ascon-80pq",
});

Decryption

const key = ...; // 16 bytes
const nonce = ...; // 16 bytes

const ciphertext = ...; // Encrypted data

// Decrypt the ciphertext using the key and the nonce
const plaintextResult = Ascon.decrypt(key, nonce, ciphertext);

const text = new TextDecoder().decode(plaintextResult); // "ascon"

ascon-js's People

Contributors

simolation avatar 0xsilkweave 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.