source: ether_megapoli/trunk/common/implementation/com/ether/EtherHelper.java @ 279

Last change on this file since 279 was 279, checked in by vmipsl, 13 years ago

Encrypt password

File size: 6.2 KB
Line 
1package com.ether;
2
3import org.apache.commons.lang.ArrayUtils;
4import org.jetbrains.annotations.NotNull;
5import org.jetbrains.annotations.Nullable;
6
7import java.io.BufferedWriter;
8import java.io.FileWriter;
9import java.io.IOException;
10import java.security.DigestException;
11import java.security.MessageDigest;
12import java.security.NoSuchAlgorithmException;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.Random;
16
17/**
18 * @author vmipsl
19 * @date 09 sept. 2011
20 */
21public abstract class EtherHelper
22{
23    /**
24     * This method returns the list with the firts values of a list of <Pair>
25     *
26     * @param pairList
27     * @return
28     */
29    @NotNull
30    public static <T1, T2> List getFirstValues( @NotNull final List<Pair<T1, T2>> pairList )
31    {
32        final List firstValues = new ArrayList<T1>( pairList.size() );
33        for( final Pair pair : pairList )
34            firstValues.add( pair.getFirstValue() );
35
36        return firstValues;
37    }
38
39    /**
40     * This method returns the list with the second values of a list of <Pair>
41     *
42     * @param pairList
43     * @return
44     */
45    @NotNull
46    public static <T1, T2> List getSecondValues( @NotNull final List<Pair<T1, T2>> pairList )
47    {
48        final List secondValues = new ArrayList<T2>( pairList.size() );
49        for( final Pair pair : pairList )
50            secondValues.add( pair.getSecondValue() );
51
52        return secondValues;
53    }
54
55    public static double[] convertListDoubleToDoubleArray( @NotNull final List<Double> list )
56    {
57        Double[] doubleArray = new Double[list.size()];
58        doubleArray = list.toArray( doubleArray );
59        return ArrayUtils.toPrimitive( doubleArray );
60    }
61
62    @NotNull
63    public static Integer getRandomBetweenTwoInteger( @NotNull final Integer minValue, @NotNull final Integer maxValue )
64    {
65        final Random r = new Random();
66        return minValue + r.nextInt( maxValue - minValue );
67    }
68
69    @NotNull
70    public static Double getRandomBetweenTwoDouble( @NotNull final Double minValue, @NotNull final Double maxValue )
71    {
72        final Random r = new Random();
73        final Double value = minValue + r.nextDouble();
74        if( value > maxValue )
75            return getRandomBetweenTwoDouble( minValue, maxValue );
76        else
77            return value;
78    }
79
80    // TODO : supprimer cette méthode et utiliser JSON pour récupérer pIdPIdList
81    @Nullable
82    public static <T1 extends Object, T2 extends Object> List<Pair<T1, T2>> extractpfIdPIdListFromString( @NotNull final String pfIdPIdArrayString, @NotNull final Class<T1> T1Class, @NotNull final Class<T2> T2Class )
83    {
84        if( pfIdPIdArrayString.isEmpty() )
85            return null;
86
87        final String[] pfIDPIdArray = pfIdPIdArrayString.split( "," );
88        final List<Pair<T1, T2>> pfIdPIdList = new ArrayList<Pair<T1, T2>>( pfIDPIdArray.length );
89        for( final String pfIdPId : pfIDPIdArray )
90        {
91            final String[] pfIdPIdSplit = pfIdPId.split( "-" );
92            final Pair<T1, T2> pfIdPIdPair = new Pair<T1, T2>();
93            if( T1Class.equals( String.class ) )
94                pfIdPIdPair.setFirstValue( (T1) pfIdPIdSplit[0] );
95            else
96                pfIdPIdPair.setFirstValue( (T1) Integer.valueOf( pfIdPIdSplit[0] ) );
97
98            if( T2Class.equals( String.class ) )
99                pfIdPIdPair.setSecondValue( (T2) pfIdPIdSplit[1] );
100            else
101                pfIdPIdPair.setSecondValue( (T2) Integer.valueOf( pfIdPIdSplit[1] ) );
102            pfIdPIdList.add( pfIdPIdPair );
103        }
104        return pfIdPIdList;
105    }
106
107    /**
108     * This method writes the text into the file filePath
109     *
110     * @param filePath
111     * @param text
112     */
113    public static void writeInFile( @NotNull final String filePath, @NotNull final String text )
114    {
115        try
116        {
117            final FileWriter fw = new FileWriter( filePath, true );
118            final BufferedWriter output = new BufferedWriter( fw );
119            output.write( text );
120            output.flush();
121            output.close();
122        }
123        catch( IOException ioe )
124        {
125            ioe.printStackTrace();
126        }
127
128    }
129
130    /**
131     * This method rounded a value with 'n' number after comma
132     *
133     * @param value            : the value to convert
134     * @param numberOfDecimals : the number of decimals after the comma
135     * @return the rounding value
136     */
137    public static Double floor( final Double value, final int numberOfDecimals )
138    {
139        final Double powValue = Math.pow( 10.0, numberOfDecimals );
140        return Math.floor( ( value * powValue ) + 0.5 ) / powValue;
141    }
142
143
144    /**
145     * Returns the specified String as an encrypted hash
146     *
147     * @param stringToEncrypt
148     * @return
149     */
150    @NotNull
151    public static byte[] encryptStringToHash( @NotNull final String stringToEncrypt )
152            throws DigestException, NoSuchAlgorithmException
153    {
154        final byte[] input = stringToEncrypt.getBytes();
155        final MessageDigest digest = MessageDigest.getInstance( "SHA-1" );
156        digest.update( input, 0, input.length );
157        final int hashLength = 20; // SHA-1 donne un hash de longueur 20
158        final byte[] hash = new byte[hashLength];
159        digest.digest( hash, 0, hashLength );
160        return hash;
161    }
162
163    /**
164     * Returns the specified bytes array hash as a String
165     *
166     * @param encryptedHash the hash as a bytes array
167     * @return the hash as a String
168     */
169    @NotNull
170    public static String displayEncryptedHash( @NotNull final byte[] encryptedHash )
171    {
172        final StringBuilder stringBuilder = new StringBuilder();
173        for( final byte aHash : encryptedHash )
174        {
175            final int v = aHash & 0xFF;
176            if( v < 16 )
177                stringBuilder.append( '0' );
178
179            stringBuilder.append( Integer.toString( v, 16 ) );
180        }
181        return stringBuilder.toString();
182    }
183
184    /**
185     * This method creates a encrypted password with SHA-1 hash
186     *
187     * @param password
188     * @return
189     */
190    @NotNull
191    public static String encryptPassword( @NotNull final String password )
192            throws DigestException, NoSuchAlgorithmException
193    {
194        final byte[] sHAPassword = EtherHelper.encryptStringToHash( password );
195        return EtherHelper.displayEncryptedHash( sHAPassword );
196    }
197
198}
Note: See TracBrowser for help on using the repository browser.