Parcourir la source

Add an OCaml native SHA1 implementation in macros (#6890)

Alfredo Beaumont il y a 7 ans
Parent
commit
bd8970630d
3 fichiers modifiés avec 19 ajouts et 2 suppressions
  1. 1 1
      Makefile
  2. 2 1
      opam
  3. 16 0
      src/macro/eval/evalStdLib.ml

+ 1 - 1
Makefile

@@ -31,7 +31,7 @@ STATICLINK?=0
 HAXE_DIRECTORIES=core syntax context codegen codegen/gencommon generators optimization filters macro macro/eval typing compiler
 EXTLIB_LIBS=extlib-leftovers extc neko javalib swflib ttflib ilib objsize pcre ziplib
 OCAML_LIBS=unix str threads dynlink
-OPAM_LIBS=sedlex xml-light extlib rope ptmap
+OPAM_LIBS=sedlex xml-light extlib rope ptmap sha
 
 FINDLIB_LIBS=$(OCAML_LIBS)
 FINDLIB_LIBS+=$(OPAM_LIBS)

+ 2 - 1
opam

@@ -20,8 +20,9 @@ depends: [
   "extlib"       {build & >= "1.7"}
   "rope"         {build}
   "ptmap"        {build}
+  "sha"
   "conf-libpcre"
   "conf-zlib"
   "conf-neko"
 ]
-available: [ ocaml-version > "4.02" ]
+available: [ ocaml-version > "4.02" ]

+ 16 - 0
src/macro/eval/evalStdLib.ml

@@ -1689,6 +1689,18 @@ module StdResource = struct
 	)
 end
 
+module StdSha1 = struct
+	let encode = vfun1 (fun s ->
+		let s = decode_string s in
+		encode_string (Sha1.to_hex (Sha1.string s))
+	)
+
+	let make = vfun1 (fun b ->
+		let b = decode_bytes b in
+		encode_bytes (Bytes.unsafe_of_string (Sha1.to_bin (Sha1.string (Bytes.unsafe_to_string b))))
+	)
+end
+
 module StdSocket = struct
 	open Unix
 
@@ -2955,6 +2967,10 @@ let init_standard_library builtins =
 		"getString",StdResource.getString;
 		"getBytes",StdResource.getBytes;
 	] [];
+	init_fields builtins (["haxe";"crypto"],"Sha1") [
+		"encode",StdSha1.encode;
+		"make",StdSha1.make;
+	] [];
 	init_fields builtins (["sys";"net";"_Socket"],"NativeSocket") [
 		"select",StdSocket.select;
 	] [