1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.password

File PasswordToByteConverter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
15
5
2
109
42
7
0.47
3
2.5
1.4

Classes

Class Line # Actions
PasswordToByteConverter 30 15 0% 7 1
0.9595%
PasswordToByteConverter.ToBytesMode 40 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.password;
21   
22    import org.bouncycastle.crypto.PBEParametersGenerator;
23   
24    /**
25    * Helper class to convert password/passphrase to bytes arrays.
26    *
27    * @version $Id: 515a078d5143f46a2e11a6180ac7290c6cca2d3e $
28    * @since 5.4M1
29    */
 
30    public final class PasswordToByteConverter
31    {
 
32  0 toggle private PasswordToByteConverter()
33    {
34    // Utility class, shouldn't be instantiated
35    }
36   
37    /**
38    * Conversion mode of password from String to byte array. Defaults to is to use PKCS5_UTF8.
39    */
 
40    public enum ToBytesMode
41    {
42    /** converts according to the scheme in PKCS5 (UTF-8, no padding). */
43    PKCS5_UTF8,
44    /** converts according to the scheme in PKCS5 (ascii, no padding). */
45    PKCS5,
46    /** converts according to the scheme in PKCS12 (unicode, big endian, 2 zero pad bytes at the end). */
47    PKCS12
48    }
49   
50    /**
51    * Convert password to bytes.
52    *
53    * @param password password to convert.
54    * @return a bytes array representing the password.
55    */
 
56  14 toggle public static byte[] convert(String password)
57    {
58  14 return convert(password.toCharArray());
59    }
60   
61    /**
62    * Convert password to bytes.
63    *
64    * @param password password to convert.
65    * @return a bytes array representing the password.
66    */
 
67  14 toggle public static byte[] convert(char[] password)
68    {
69  14 return convert(password, ToBytesMode.PKCS5_UTF8);
70    }
71   
72    /**
73    * Convert password to bytes.
74    *
75    * @param password password to convert.
76    * @param mode mode of conversion.
77    * @return a bytes array representing the password.
78    */
 
79  3 toggle public static byte[] convert(String password, ToBytesMode mode)
80    {
81  3 return convert(password.toCharArray(), mode);
82    }
83   
84    /**
85    * Convert password to bytes.
86    *
87    * @param password password to convert.
88    * @param mode mode of conversion.
89    * @return a bytes array representing the password.
90    */
 
91  17 toggle public static byte[] convert(char[] password, ToBytesMode mode)
92    {
93  17 byte[] passwd;
94   
95  17 switch (mode) {
96  1 case PKCS12:
97  1 passwd = PBEParametersGenerator.PKCS12PasswordToBytes(password);
98  1 break;
99  2 case PKCS5:
100  2 passwd = PBEParametersGenerator.PKCS5PasswordToBytes(password);
101  2 break;
102  14 default:
103  14 passwd = PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
104  14 break;
105    }
106   
107  17 return passwd;
108    }
109    }