2020
2121import com .cloud .utils .db .Encrypt ;
2222import com .cloud .utils .exception .CloudRuntimeException ;
23+ import org .apache .commons .lang3 .StringUtils ;
2324
2425import javax .persistence .Column ;
2526import javax .persistence .Entity ;
2627import javax .persistence .GeneratedValue ;
2728import javax .persistence .GenerationType ;
2829import javax .persistence .Id ;
2930import javax .persistence .Table ;
31+
3032import java .security .NoSuchAlgorithmException ;
3133import java .security .SecureRandom ;
3234import java .util .Arrays ;
@@ -42,32 +44,39 @@ public class PassphraseVO {
4244
4345 @ Column (name = "passphrase" )
4446 @ Encrypt
45- private byte [] passphrase ;
47+ private String passphrase ;
4648
4749 public PassphraseVO () {
48- try {
49- SecureRandom random = SecureRandom .getInstanceStrong ();
50- byte [] temporary = new byte [48 ]; // 48 byte random passphrase buffer
51- this .passphrase = new byte [64 ]; // 48 byte random passphrase as base64 for usability
52- random .nextBytes (temporary );
53- Base64 .getEncoder ().encode (temporary , this .passphrase );
54- Arrays .fill (temporary , (byte ) 0 ); // clear passphrase from buffer
55- } catch (NoSuchAlgorithmException ex ) {
56- throw new CloudRuntimeException ("Volume encryption requested but system is missing specified algorithm to generate passphrase" );
50+ }
51+
52+ public PassphraseVO (boolean initialize ) {
53+ if (initialize ) {
54+ try {
55+ SecureRandom random = SecureRandom .getInstanceStrong ();
56+ byte [] temporary = new byte [48 ]; // 48 byte random passphrase buffer
57+ random .nextBytes (temporary );
58+ this .passphrase = Base64 .getEncoder ().encodeToString (temporary );
59+ Arrays .fill (temporary , (byte ) 0 ); // clear passphrase from buffer
60+ } catch (NoSuchAlgorithmException ex ) {
61+ throw new CloudRuntimeException ("Volume encryption requested but system is missing specified algorithm to generate passphrase" );
62+ }
5763 }
5864 }
5965
6066 public PassphraseVO (PassphraseVO existing ) {
61- this .passphrase = existing .getPassphrase ();
67+ this .passphrase = existing .getPassphraseString ();
6268 }
6369
64- public void clearPassphrase () {
65- if (this .passphrase != null ) {
66- Arrays . fill ( this . passphrase , ( byte ) 0 ) ;
70+ public byte [] getPassphrase () {
71+ if (StringUtils . isBlank ( this .passphrase ) ) {
72+ return new byte []{} ;
6773 }
74+ return this .passphrase .getBytes ();
6875 }
6976
70- public byte [] getPassphrase () { return this .passphrase ; }
77+ public String getPassphraseString () {
78+ return this .passphrase ;
79+ }
7180
7281 public Long getId () { return this .id ; }
7382}
0 commit comments