watchdog sign
This commit is contained in:
parent
e4877d18b0
commit
e63244dbac
@ -223,7 +223,7 @@ format_warn(Subject, Users, PriKey) when is_binary(Subject), is_binary(Users) ->
|
|||||||
<<"nodeName">> => <<"主机监控"/utf8>>,
|
<<"nodeName">> => <<"主机监控"/utf8>>,
|
||||||
<<"sysId">> => ?SYS_ID
|
<<"sysId">> => ?SYS_ID
|
||||||
},
|
},
|
||||||
Sign = iot_jinzhi_signer:sign(PostParams, PriKey),
|
Sign = sign(PostParams, PriKey),
|
||||||
jiffy:encode(PostParams#{<<"sign">> => Sign}, [force_utf8]).
|
jiffy:encode(PostParams#{<<"sign">> => Sign}, [force_utf8]).
|
||||||
|
|
||||||
-spec do_post(Url :: string(), Body :: binary()) -> {ok, Resp :: any()} | {error, Reason :: binary()}.
|
-spec do_post(Url :: string(), Body :: binary()) -> {ok, Resp :: any()} | {error, Reason :: binary()}.
|
||||||
@ -296,4 +296,43 @@ format_date() ->
|
|||||||
{Date, Time} = calendar:now_to_local_time(os:timestamp()),
|
{Date, Time} = calendar:now_to_local_time(os:timestamp()),
|
||||||
{{Year, Month, Day}, {Hour, Minute, Second}} = {Date, Time},
|
{{Year, Month, Day}, {Hour, Minute, Second}} = {Date, Time},
|
||||||
DateTime = io_lib:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B", [Year, Month, Day, Hour, Minute, Second]),
|
DateTime = io_lib:format("~4..0B-~2..0B-~2..0B ~2..0B:~2..0B:~2..0B", [Year, Month, Day, Hour, Minute, Second]),
|
||||||
list_to_binary(DateTime).
|
list_to_binary(DateTime).
|
||||||
|
|
||||||
|
|
||||||
|
%% 数据签名
|
||||||
|
-spec sign(M :: #{}, PrivateKey :: public_key:private_key()) -> binary().
|
||||||
|
sign(M, PrivateKey) when is_map(M) ->
|
||||||
|
Json = serialize(M),
|
||||||
|
Hash = iolist_to_binary(io_lib:format("~64.16.0b", [binary:decode_unsigned(crypto:hash(sha256, Json))])),
|
||||||
|
RsaEncoded = public_key:encrypt_private(Hash, PrivateKey),
|
||||||
|
base64:encode(RsaEncoded).
|
||||||
|
|
||||||
|
%% 简单的序列号,sign签名
|
||||||
|
-spec serialize(M :: map()) -> JsonString :: binary().
|
||||||
|
serialize(M) when is_map(M) ->
|
||||||
|
L = maps:to_list(M),
|
||||||
|
L1 = lists:sort(fun({K, _}, {K1, _}) -> K < K1 end, L),
|
||||||
|
serialize0(L1, []).
|
||||||
|
|
||||||
|
serialize0([], Target) ->
|
||||||
|
B = iolist_to_binary(lists:join(<<$,>>, lists:reverse(Target))),
|
||||||
|
<<${, B/binary, $}>>;
|
||||||
|
serialize0([{K, V}|T], Target) ->
|
||||||
|
V1 = if
|
||||||
|
is_integer(V) ->
|
||||||
|
integer_to_binary(V);
|
||||||
|
is_float(V) ->
|
||||||
|
float_to_binary(V);
|
||||||
|
is_binary(V) ->
|
||||||
|
<<$", V/binary, $">>;
|
||||||
|
is_boolean(V) andalso V ->
|
||||||
|
<<"true">>;
|
||||||
|
is_boolean(V) andalso not V ->
|
||||||
|
<<"false">>;
|
||||||
|
is_list(V) ->
|
||||||
|
Items = lists:map(fun(E) -> serialize(E) end, V),
|
||||||
|
V0 = iolist_to_binary(lists:join(<<$,>>, Items)),
|
||||||
|
<<$[, V0/binary, $]>>
|
||||||
|
end,
|
||||||
|
Item = <<$", K/binary, $", $:, V1/binary>>,
|
||||||
|
serialize0(T, [Item|Target]).
|
||||||
Loading…
x
Reference in New Issue
Block a user