Coder Social home page Coder Social logo

malmaud / nettle.jl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juliacrypto/nettle.jl

0.0 2.0 0.0 93 KB

Julia wrapper around nettle cryptographic hashing/encryption library providing MD5, SHA1, SHA2 hashing and HMAC functionality, as well as AES encryption/decryption

License: Other

Julia 100.00%

nettle.jl's Introduction

Nettle.jl

Build Status Build status

libnettle supports a wide array of hashing algorithms. This package interrogates libnettle to determine the available hash types, which are then available from Nettle.get_hash_types(). Typically these include SHA1, SHA224, SHA256, SHA384, SHA512, MD2, MD5 and RIPEMD160.

Typical usage of these hash algorithms is to create a Hasher, update! it, and finally get a digest:

h = Hasher("sha256")
update!(h, "this is a test")
hexdigest!(h)

#or...
hexdigest("sha256", "this is a test")

Outputs:

2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c

A digest! function is also available to return the digest as an Array(UInt8,1). Note that both the digest! function and the hexdigest! function reset the internal Hasher object to a pristine state, ready for further update! calls.

HMAC Functionality

HMAC functionality revolves around the HMACState type, created by the function of the same name. Arguments to this constructor are the desired hash type, and the desired key used to authenticate the hashing:

h = HMACState("sha256", "mykey")
update!(h, "this is a test")
hexdigest!(h)

#or...
hexdigest("sha256", "mykey", "this is a test")

Outputs:

"ca1dcafe1b5fb329256248196c0f92a95fbe3788db6c5cb0775b4106db437ba2"

A digest! function is also available to return the digest as an Array(UInt8,1). Note that both the digest! function and the hexdigest! function reset the internal HMACState object to a pristine state, ready for further update! calls.

Encryption/Decryption Functionality

Nettle also provides encryption and decryption functionality, using the Encryptor and Decryptor objects. Cipher types are available through get_cipher_types(). Create a pair of objects with a shared key, and encrypt()/decrypt() to your heart's content:

key = "this key's exactly 32 bytes long"
enc = Encryptor("AES256", key)
plaintext = "this is 16 chars"
ciphertext = encrypt(enc, plaintext.data)

dec = Decryptor("AES256", key)
deciphertext = decrypt(dec, ciphertext)
plaintext.data == deciphertext # no bytestring

# or...
decrypt("AES256", key, encrypt("AES256", key, plaintext)) == plaintext.data

For AES256CBC encrypt/decrypt, generate a pair of key32 and iv16 with salt.

(And add or trim padding yourself.)

passwd = "Secret Passphrase"
salt = hex2bytes("a3e550e89e70996c") # use random 8 bytes
(key32, iv16) = gen_key32_iv16(passwd.data, salt)

enc = Encryptor("AES256", key32)
plaintext = "Message"
ciphertext = encrypt(enc, :CBC, iv16, add_padding_PKCS5(plaintext.data, 16))

dec = Decryptor("AES256", key32)
deciphertext = decrypt(dec, :CBC, iv16, ciphertext)
plaintext.data == trim_padding_PKCS5(deciphertext) # no bytestring

# or...
plainbytes = hex2bytes("414155aa5541416162")
cipherbytes = encrypt("AES256", :CBC, iv16, key32, add_padding_PKCS5(plainbytes, 16))
decipherbytes = decrypt("AES256", :CBC, iv16, key32, cipherbytes)
plainbytes == trim_padding_PKCS5(decipherbytes) # no bytestring

nettle.jl's People

Contributors

staticfloat avatar hatsunemiku avatar tkelman avatar yuyichao avatar samoconnor avatar stevengj avatar keno avatar ihnorton avatar jiahao avatar samschlegel avatar stefankarpinski avatar abhijithch avatar binarybana avatar kmsquire avatar swt30 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.