Erlang的并行编译与加密

1.Erlang防止反编译 , 提供encrypt_debug_info 参数。

abstract_code(#compile{code=Code,options=Opts,ofile=OFile}) ->
    Abstr = erlang:term_to_binary({raw_abstract_v1,Code}, [compressed]),
    case member(encrypt_debug_info, Opts) of %%查找EmakeFile里是否带有这个参数。 例如 [encrypt_debug_info,{debug_info_key,"testkey"}]
	true ->
	    case keyfind(debug_info_key, 1, Opts) of
		{_,Key} ->
		    encrypt_abs_code(Abstr, Key); %%有的话当然直接去加密,erlang加密算法 des3_cbc , 3DES加密
		false ->
		    %% Note: #compile.module has not been set yet.
		    %% Here is an approximation that should work for
		    %% all valid cases.
		    %%如果没有则会去文件中查找,注释的代码在 beam_lib.erl、
		    %%	crypto_key_fun_from_file() ->
                    %%      case init:get_argument(home) of %%init:get_argument(home) ,home目录查找,如果没有home那则在当前目录
	            %%          {ok,[[Home]]} ->
		    %%		    crypto_key_fun_from_file_1([".",Home]);
		    %%	    _ ->
		    %%	       crypto_key_fun_from_file_1(["."])
		    %%  end.
		    %% crypto_key_fun_from_file_1(Path) ->
		    %%	case f_p_s(Path, ".erlang.crypt") of %%默认查找.erlang.crypt
		    %%		{ok, KeyInfo, _} ->
		    %%		   try_load_crypto_fun(KeyInfo);
		    %%	        _ ->
		    %%	             error
		    %% end.             
		    Module = list_to_atom(filename:rootname(filename:basename(OFile))),
		    Mode = proplists:get_value(crypto_mode, Opts, des3_cbc),
		    case beam_lib:get_crypto_key({debug_info, Mode, Module, OFile}) of
			error ->
			    {error, [{none,?MODULE,no_crypto_key}]}; %%报错没有密钥
			Key ->
			    encrypt_abs_code(Abstr, {Mode, Key})
		    end
	    end;
	false ->
	    {ok, Abstr}
    end.
1. 可以在EmakeFile里指定debug_info_key

2.win下可在当前目录,或者我的文档目录创建 .erlang.crypt  ->  [{debug_info, des3_cbc, [], "li&^R^%0"}].  linux同理

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%分割线 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

并行编译

mmake.erl  首先要编译 mmake.erl

执行代码:

mmake.erl 目录自己定义

compile_mmake()->
	MMake = case filelib:wildcard("../src/mmake.erl") of
				[]-> 
					case filelib:wildcard("../src/*/*/mmake.erl") of  
						[]-> [];
						[MMakeF]->MMakeF
					end
			end,


	case make:files([MMake],[{outdir, "../ebin"}]) of
		error-> io:format("can not compile mmake.erl\n"),
				halt(1);
		_->	
			ok
	end.
get_cpu_cores()->
	erlang:min(erlang:system_info(logical_processors)-1,1). 
	
compile_all(Options)->
	case mmake:all(get_cpu_cores(),[Options]) of 
		up_to_date ->
			halt(0); 
		error -> 
			halt(1) 
	end.
compile_all()->
	code:add_patha("../ebin"),
	case mmake:all(get_cpu_cores()) of 
		up_to_date ->
			halt(0); 
		error -> 
			halt(1) 
	end.

感谢 

http://erlangdisplay.iteye.com/blog/1264038 

http://blog.csdn.net/phyzhou/article/details/8954931 两位作者。

你可能感兴趣的:(erlang)