forked from paixaop/node-sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_hash_sha256.cc
96 lines (75 loc) · 2.58 KB
/
crypto_hash_sha256.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Node Native Module for Lib Sodium
*
* @Author Pedro Paixao
* @email paixaop at gmail dot com
* @License MIT
*/
#include "node_sodium.h"
/**
* int crypto_hash_sha256(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash_sha256) {
Nan::EscapableHandleScope scope;
ARGS(1,"argument message must be a buffer");
ARG_TO_UCHAR_BUFFER(msg);
NEW_BUFFER_AND_PTR(hash, crypto_hash_sha256_BYTES);
if( crypto_hash_sha256(hash_ptr, msg, msg_size) == 0 ) {
return info.GetReturnValue().Set(hash);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
* int crypto_hash_sha256_init(crypto_hash_sha256_state *state);
*/
NAN_METHOD(bind_crypto_hash_sha256_init) {
Nan::EscapableHandleScope scope;
NEW_BUFFER_AND_PTR(state, crypto_hash_sha256_statebytes());
if( crypto_hash_sha256_init((crypto_hash_sha256_state*) state_ptr) == 0 ) {
return info.GetReturnValue().Set(state);
}
return info.GetReturnValue().Set(Nan::Null());
}
/* int crypto_hash_sha256_update(crypto_hash_sha256_state *state,
const unsigned char *in,
unsigned long long inlen);
Buffer state
Buffer inStr
*/
NAN_METHOD(bind_crypto_hash_sha256_update) {
Nan::EscapableHandleScope scope;
ARGS(2,"arguments must be two buffers: hash state, message part");
ARG_TO_VOID_BUFFER(state);
ARG_TO_UCHAR_BUFFER(msg);
if( crypto_hash_sha256_update((crypto_hash_sha256_state*)state, msg, msg_size) == 0 ) {
return info.GetReturnValue().Set(Nan::True());
}
return info.GetReturnValue().Set(Nan::False());
}
/* int crypto_hash_sha256_final(crypto_hash_sha256_state *state,
unsigned char *out);
*/
NAN_METHOD(bind_crypto_hash_sha256_final) {
Nan::EscapableHandleScope scope;
ARGS(1,"arguments must be a hash state buffer");
ARG_TO_VOID_BUFFER(state);
NEW_BUFFER_AND_PTR(hash, crypto_hash_sha256_BYTES);
if( crypto_hash_sha256_final((crypto_hash_sha256_state*)state, hash_ptr) == 0 ) {
return info.GetReturnValue().Set(hash);
}
return info.GetReturnValue().Set(Nan::False());
}
/**
* Register function calls in node binding
*/
void register_crypto_hash_sha256(Handle<Object> target) {
// Hash
NEW_METHOD(crypto_hash_sha256);
NEW_METHOD(crypto_hash_sha256_init);
NEW_METHOD(crypto_hash_sha256_update);
NEW_METHOD(crypto_hash_sha256_final);
NEW_INT_PROP(crypto_hash_sha256_BYTES);
}