LightJason - AgentSpeak(L++)
action/builtin/crypto/CHash.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin.crypto;
25 
26 import com.google.common.hash.Hashing;
27 import com.google.common.io.BaseEncoding;
28 import org.apache.commons.lang3.SerializationUtils;
37 
38 import javax.annotation.Nonnegative;
39 import javax.annotation.Nonnull;
40 import java.io.Serializable;
41 import java.io.UnsupportedEncodingException;
42 import java.security.MessageDigest;
43 import java.security.NoSuchAlgorithmException;
44 import java.util.List;
45 import java.util.Locale;
46 
47 
65 public final class CHash extends IBuiltinAction
66 {
70  private static final long serialVersionUID = 4638666396527392307L;
71 
72  @Nonnegative
73  @Override
74  public final int minimalArgumentNumber()
75  {
76  return 2;
77  }
78 
79  @Nonnull
80  @Override
81  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
82  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
83  )
84  {
85  CCommon.flatten( p_argument )
86  .skip( 1 )
87  .map( i -> hash( p_context, p_argument.get( 0 ).<String>raw(), serialize( p_context, i ) ) )
88  .map( CRawTerm::from )
89  .forEach( p_return::add );
90 
91  return CFuzzyValue.from( true );
92  }
93 
94 
104  private static byte[] serialize( @Nonnull final IContext p_context, @Nonnull final ITerm p_object )
105  {
106  try
107  {
108  return CCommon.rawvalueAssignableTo( p_object, String.class )
109  ? p_object.<String>raw().getBytes( "UTF-8" )
110  : SerializationUtils.serialize( p_object.<Serializable>raw() );
111  }
112  catch ( final UnsupportedEncodingException l_exception )
113  {
114  throw new CRuntimeException( l_exception, p_context );
115  }
116  }
117 
126  private static String hash( @Nonnull final IContext p_context, @Nonnull final String p_algorithm, @Nonnull final byte[] p_data )
127  {
128  switch ( p_algorithm.trim().toLowerCase( Locale.ROOT ) )
129  {
130  case "adler-32":
131  return Hashing.adler32().newHasher().putBytes( p_data ).hash().toString();
132 
133  case "crc-32":
134  return Hashing.crc32().newHasher().putBytes( p_data ).hash().toString();
135 
136  case "crc-32c":
137  return Hashing.crc32c().newHasher().putBytes( p_data ).hash().toString();
138 
139  case "murmur3-32":
140  return Hashing.murmur3_32().newHasher().putBytes( p_data ).hash().toString();
141 
142  case "murmur3-128":
143  return Hashing.murmur3_128().newHasher().putBytes( p_data ).hash().toString();
144 
145  case "siphash-2-4":
146  return Hashing.sipHash24().newHasher().putBytes( p_data ).hash().toString();
147 
148  default:
149  try
150  {
151  return BaseEncoding.base16().encode( MessageDigest.getInstance( p_algorithm ).digest( p_data ) ).toLowerCase( Locale.ROOT );
152  }
153  catch ( final NoSuchAlgorithmException l_exception )
154  {
155  throw new CRuntimeException( l_exception, p_context );
156  }
157  }
158  }
159 
160 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
static byte [] serialize( @Nonnull final IContext p_context, @Nonnull final ITerm p_object)
serialize data
common structure for execution definition
static String hash( @Nonnull final IContext p_context, @Nonnull final String p_algorithm, @Nonnull final byte[] p_data)
runs hashing function with difference between Google Guava hashing and Java default digest ...
execution context with local data
Definition: IContext.java:42
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45