Skip to content

Commit

Permalink
decode
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Nov 21, 2024
1 parent f05b502 commit 9b3bffc
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/eudi/mdoc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ defmodule CA.MDoc do
def parseMDocHex(x) do {:ok, hex} = :file.read_file x ; decode(:oid.unhex(hex)) end

def testMDoc(x) do
x = :erlang.iolist_to_binary(x)
{:ok, bin} = :file.read_file x
case :filename.extension(x) do
'.b64' -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(bin))), name: x]
'.b64u' -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(replace(replace(bin,"_","/"),"-","+")))), name: x]
'.hex' -> [bin: byte_size(:erlang.term_to_binary(:oid.unhex(bin))), name: x]
".b64" -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(bin))), name: x]
".b64u" -> [bin: byte_size(:erlang.term_to_binary(:base64.decode(replace(replace(bin,"_","/"),"-","+")))), name: x]
".hex" -> [bin: byte_size(:erlang.term_to_binary(:oid.unhex(bin))), name: x]
end
end

Expand All @@ -40,14 +41,17 @@ defmodule CA.MDoc do
version = :maps.get("version", map, [])
validityInfo = :maps.get("validityInfo", map, [])
valueDigests = :maps.get("valueDigests", map, [])
[{_,nameSpacesList}] = :maps.to_list(valueDigests)
digests = case :maps.to_list(valueDigests) do
x when is_list(x) -> :lists.map(fn a -> :lists.map(fn {_,b} -> parseTag(b) end, :maps.to_list(a)) end, x)
x -> x
end
[
docType: docType,
digestAlgorithm: da,
deviceKeyInfo: dki,
validityInfo: validityInfo,
status: status,
valueDigests: :lists.map(fn x -> parseTag(x) end, nameSpacesList),
valueDigests: digests,
version: version,
]
end
Expand Down Expand Up @@ -87,26 +91,28 @@ defmodule CA.MDoc do

def parseTag(%CBOR.Tag{tag: type1, value: %CBOR.Tag{tag: type2, value: bytes}}) when is_binary(bytes) do
{:ok, cbor, _} = decode(bytes)
[tag: {type1, type2}, decoded: true, value: parseTag(cbor)]
[tag: {type1, type2}, decoded: true, value: parseMapValue(cbor)]
end

def parseTag(%CBOR.Tag{tag: type, value: bytes}) when is_binary(bytes) do
{:ok, cbor, _} = decode(bytes)
[tag: type, decoded: true, value: parseTag(cbor)]
[tag: type, decoded: true, value: cbor]
end

def parseTag(map) when is_map(map) do
case :maps.get("docType", map, []) do
[] -> case :maps.get("elementIdentifier", map, []) do
[] -> map
_ -> parseDigest(map)
end
_ -> parseDocType(map)
end
end
def parseTag(value) when is_binary(value) do :base64.encode(value) end

def parseTag(value) do value end

def parseMapValue(map) when is_map(map) do
case :maps.get("elementIdentifier", map, []) do
[] -> case :maps.get("deviceKeyInfo", map, []) do
[] -> map
_ -> parseDocType(map)
end
_ -> parseDigest(map)
end
end

def parseMDoc(%{"deviceAuth"=> %{"deviceSignature" => tags}, "nameSpaces"=> ns}) do
[
deviceAuth: [deviceSignature: :lists.map(fn x -> parseTag(x) end, tags)],
Expand All @@ -133,7 +139,7 @@ defmodule CA.MDoc do
]
end

def parseMDoc(%{"documents"=>documents}) do
def parseMDoc(%{"documents" => documents}) do
:lists.map(fn x -> parseMDoc(x) end, documents)
end

Expand All @@ -142,16 +148,17 @@ defmodule CA.MDoc do
end

def parseMDocEnvelop(x) do
x = :erlang.iolist_to_binary(x)
{:ok, bin} = :file.read_file x

Check warning on line 152 in lib/eudi/mdoc.ex

View workflow job for this annotation

GitHub Actions / build

variable "bin" is unused (if the variable is not meant to be used, prefix it with an underscore)
case :filename.extension(x) do
".b64" -> {:ok, mDoc, _} = parseMDocB64(x) ; parseMDoc(mDoc)
".b64" -> {:ok, mDoc, _} = parseMDocB64(x) ; parseMDoc(mDoc)
".b64u" -> {:ok, mDoc, _} = parseMDocB64U(x) ; parseMDoc(mDoc)
".hex" -> {:ok, mDoc, _} = parseMDocHex(x) ; parseMDoc(mDoc)
".hex" -> {:ok, mDoc, _} = parseMDocHex(x) ; parseMDoc(mDoc)
end
end

def test(folder \\ "cbor") do
:lists.map(fn x -> parseMDoc(x) end, :filelib.wildcard ['test/#{folder}/*'])
def test(folder \\ "mdoc") do
:lists.map(fn x -> testMDoc(x) end, :filelib.wildcard ['test/#{folder}/*'])
end

end

0 comments on commit 9b3bffc

Please sign in to comment.