Skip to content

Commit

Permalink
Merge pull request #2 from erlangpack/otp24
Browse files Browse the repository at this point in the history
OTP24 compatibility
  • Loading branch information
mworrell authored Jun 17, 2021
2 parents e0e199f + aafaeeb commit 7b7cbc4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 61 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/hex-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Hex Publish

on:
push:
tags:
- '*'

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v2

- name: Publish to Hex.pm
uses: erlangpack/github-action@v1
env:
HEX_API_KEY: ${{ secrets.HEX_API_KEY }}
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This workflow checks the tests and dialyzer.

name: Test

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
linux:
name: Test on OTP ${{ matrix.otp_version }}
runs-on: ${{ matrix.os }}

strategy:
matrix:
otp_version: [22.3, 23.2.5, 24.0.1]
os: [ubuntu-latest]

container:
image: erlang:${{ matrix.otp_version }}

steps:
- uses: actions/checkout@v2
- name: Compile
run: make
- name: Test
run: make test
- name: XRef
run: make xref
- name: Dialyzer
run: make dialyzer
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ ebin
deps
.eunit
.ct
.rebar
.rebar3
rebar3
logs
test/*.beam
erl_crash.dump
_build
40 changes: 21 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
all: deps compile check test
ERL ?= erl
REBAR := ./rebar3
REBAR_URL := https://s3.amazonaws.com/rebar3/rebar3

deps:
rebar get-deps
.PHONY: all compile check test clean

compile:
rebar compile
all: ./rebar3 compile check test

$(REBAR):
$(ERL) -noshell -s inets -s ssl \
-eval '{ok, saved_to_file} = httpc:request(get, {"$(REBAR_URL)", []}, [{ssl, [ {verify, verify_none} ]}], [{stream, "$(REBAR)"}])' \
-s init stop
chmod +x $(REBAR)

run: compile
sh start.sh
compile:
$(REBAR) compile

clean:
rebar clean
rm -fr ebin .ct test/*.beam
$(REBAR) clean

check:
rebar eunit skip_deps=true

test: deps compile check
#rebar ct
mkdir -p .ct
ct_run -dir test -logdir .ct -pa ebin
$(REBAR) eunit

dist: deps compile
echo TODO
test: compile check
$(REBAR) ct

.PHONY: all deps compile check test run clean dist
.SILENT:
dialyzer:
$(REBAR) dialyzer

xref:
$(REBAR) xref
6 changes: 6 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@
]}.

{cover_enabled, true}.

{xref_checks, [
undefined_function_calls,
locals_not_used,
deprecated_function_calls
]}.
1 change: 1 addition & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[].
46 changes: 5 additions & 41 deletions src/termit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,63 +75,28 @@ decode(Bin, _) when is_binary(Bin) ->
{error, forged}.



-ifdef(crypto_compatibility).
-spec key(
Secret :: binary()) ->
MAC16 :: binary().

key(Secret) ->
crypto:md5_mac(Secret, []).

-spec sign(
Data :: binary(),
Secret :: binary()) ->
MAC20 :: binary().

sign(Data, Key) ->
crypto:sha_mac(Key, Data).

-spec encrypt(
Data :: binary(),
Key :: binary(),
IV :: binary()) ->
Cipher :: binary().

encrypt(Data, Key, IV) ->
Crypt = crypto:aes_cfb_128_encrypt(Key, IV, Data),
<< IV/binary, Crypt/binary>>.

-spec uncrypt(
Cipher :: binary(),
Key :: binary()) ->
Uncrypted :: binary().

uncrypt(<< IV:16/binary, Data/binary >>, Key) ->
crypto:aes_cfb_128_decrypt(Key, IV, Data).
-else
-spec key(
Secret :: binary()) ->
MAC16 :: binary().

key(Secret) ->
crypto:hmac(md5, Secret, []).
crypto:mac(hmac, md5, Secret, []).

-spec sign(
Data :: binary(),
Secret :: binary()) ->
MAC20 :: binary().

sign(Data, Key) ->
crypto:hmac(sha, Key, Data).
crypto:mac(hmac, sha, Key, Data).
-spec encrypt(
Data :: binary(),
Key :: binary(),
IV :: binary()) ->
Cipher :: binary().

encrypt(Data, Key, IV) ->
Crypt = crypto:block_encrypt(aes_cfb128, Key, IV, Data),
Crypt = crypto:crypto_one_time(aes_128_cfb128, Key, IV, Data, true),
<< IV/binary, Crypt/binary>>.

-spec uncrypt(
Expand All @@ -140,9 +105,8 @@ encrypt(Data, Key, IV) ->
Uncrypted :: binary().

uncrypt(<< IV:16/binary, Data/binary >>, Key) ->
crypto:block_decrypt(aes_cfb128, Key, IV, Data).
crypto:crypto_one_time(aes_128_cfb128, Key, IV, Data, false).

-endif.

%%
%% -----------------------------------------------------------------------------
Expand Down Expand Up @@ -286,7 +250,7 @@ rand_uniform(N) ->

encrypt_test() ->
IV = crypto:strong_rand_bytes(16),
Secret = crypto:hmac(md5, <<"Make It Elegant">>, []),
Secret = crypto:mac(hmac, md5, <<"Make It Elegant">>, []),
<< Secret15:15/binary, _/binary >> = Secret,
Bin = <<"Transire Benefaciendo">>,
?assertEqual(Bin, uncrypt(encrypt(Bin, Secret, IV), Secret)),
Expand Down

0 comments on commit 7b7cbc4

Please sign in to comment.