anchor.diff:  %%--------------------------------------------------------------------
   %% %CopyrightBegin%
   %%
  -%% Copyright Ericsson AB 2012. All Rights Reserved.
  +%% Copyright Ericsson AB 2013. All Rights Reserved.
   %%
   %% The contents of this file are subject to the Erlang Public License,
   %% Version 1.1, (the "License"); you may not use this file except in
  
positive.code:     Q  = prime(NSize),  %% Choose N-bit prime Q
      X0 = prime(LSize),
      P0 = prime((LSize div 2) +1),
  
      %% Choose L-bit prime modulus P such that p-1 is a multiple of q.
      case dsa_search(X0 div (2*Q*P0), P0, Q, 1000) of
  	error ->
  	    gen_dsa2(LSize, NSize);
  	P ->
  	    G = crypto:mod_exp(2, (P-1) div Q, P), % Choose G a number whose multiplicative order modulo p is q.
  	    %%                 such that This may be done by setting g = h^(p-1)/q mod p, commonly h=2 is used.
  
  	    X = prime(20),               %% Choose x by some random method, where 0 < x < q.
  	    Y = crypto:mod_exp(G, X, P), %% Calculate y = g^x mod p.
  
  	    #'DSAPrivateKey'{version=0, p=P, q=Q, g=G, y=Y, x=X}
      end.
  
  %% See fips_186-3.pdf
  dsa_search(T, P0, Q, Iter) when Iter > 0 ->
      P = 2*T*Q*P0 + 1,
      case is_prime(crypto:mpint(P), 50) of
  	true -> P;
  	false -> dsa_search(T+1, P0, Q, Iter-1)
      end;
  dsa_search(_,_,_,_) ->
      error.
  
  
  %%%%%%% Crypto Math %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  prime(ByteSize) ->
      Rand = odd_rand(ByteSize),
      crypto:erlint(prime_odd(Rand, 0)).
  
  prime_odd(Rand, N) ->
      case is_prime(Rand, 50) of
  	true ->
  	    Rand;
  	false ->
  	    NotPrime = crypto:erlint(Rand),
  	    prime_odd(crypto:mpint(NotPrime+2), N+1)
      end.
  
  %% see http://en.wikipedia.org/wiki/Fermat_primality_test
  is_prime(_, 0) -> true;
  is_prime(Candidate, Test) ->
      CoPrime = odd_rand(<<0,0,0,4, 10000:32>>, Candidate),
      case crypto:mod_exp(CoPrime, Candidate, Candidate) of
  	CoPrime -> is_prime(Candidate, Test-1);
  	_       -> false
      end.
  
  odd_rand(Size) ->
      Min = 1 bsl (Size*8-1),
      Max = (1 bsl (Size*8))-1,
      odd_rand(crypto:mpint(Min), crypto:mpint(Max)).
  
  odd_rand(Min,Max) ->
      Rand = <<Sz:32, _/binary>> = crypto:rand_uniform(Min,Max),
      BitSkip = (Sz+4)*8-1,
      case Rand of
  	Odd  = <<_:BitSkip,  1:1>> -> Odd;
  	Even = <<_:BitSkip,  0:1>> ->
  	    crypto:mpint(crypto:erlint(Even)+1)
      end.
  
  copyfile(SrcDir, DstDir, Fn) ->
  
negative.code: encode_key(Key = #'DSAPrivateKey'{}) ->
      Der = public_key:der_encode('DSAPrivateKey', Key),
      {'DSAPrivateKey', Der, not_encrypted}.
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %% DSA key generation  (OBS: for testing only)
  