Code Monkey home page Code Monkey logo

rust-expr-hmac-sha256-main's Introduction

Experiment with hmac-sha256

Validate that the hmac-sha256 crate generates signatures that match what binance.us expects.

The src/main.rs:

/// Test that hmac_sha256 produce the expected results
/// as defined by [binance.us api docs](https://github.com/binance-us/binance-official-api-docs).

use hmac_sha256::HMAC;

#[macro_use]
extern crate hex_literal;


fn main() -> () {
    // Data, key and expected
    // from: https://github.com/binance-us/binance-official-api-docs/blob/fc916164ae04eb2e95ff7f98c2d49d8d6bd6d096/rest-api.md#example-2-as-a-query-string
    let data = b"symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.1&recvWindow=5000&timestamp=1499827319559";
    let key = b"NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
    let expected = hex!("c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71");

    // Calculate the signature from the data and key
    let signature = HMAC::mac(data, key);
    println!("signature ={:02x?}", signature);

    // Validate
    assert_eq!(signature, expected);
}

Run

It works!

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/expr-hmac-sha256`
signature =[c8, db, 56, 82, 5a, e7, 1d, 6d, 79, 44, 78, 49, e6, 17, 11, 5f, 4a, 92, 0f, a2, ac, dc, ab, 2b, 05, 3c, 4b, 28, 38, bd, 6b, 71]

rust-expr-hmac-sha256-main's People

Contributors

mason0510 avatar

Watchers

 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.