Passwd.java
01 /*
02  *  Licensed to the Apache Software Foundation (ASF) under one
03  *  or more contributor license agreements.  See the NOTICE file
04  *  distributed with this work for additional information
05  *  regarding copyright ownership.  The ASF licenses this file
06  *  to you under the Apache License, Version 2.0 (the
07  *  "License"); you may not use this file except in compliance
08  *  with the License.  You may obtain a copy of the License at
09  *
10  *    http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing,
13  *  software distributed under the License is distributed on an
14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  *  KIND, either express or implied.  See the License for the
16  *  specific language governing permissions and limitations
17  *  under the License.    
18  *
19  
20  */
21 package org.apache.qpid.tools.security;
22 
23 import org.apache.commons.codec.binary.Base64;
24 
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.DigestException;
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.io.PrintStream;
32 
33 public class Passwd
34 {
35     public static void main(String args[]) throws NoSuchAlgorithmException, DigestException, IOException
36     {
37         if (args.length != 2)
38         {
39             System.out.println("Passwd <username> <password>");
40             System.exit(0);
41         }
42 
43         byte[] data = args[1].getBytes("utf-8");
44 
45         MessageDigest md = MessageDigest.getInstance("MD5");
46 
47         for (byte b : data)
48         {
49             md.update(b);
50         }
51 
52         byte[] digest = md.digest();
53 
54         Base64 b64 = new Base64();
55 
56         byte[] encoded = b64.encode(digest);
57 
58         output(args[0], encoded);
59     }
60 
61     private static void output(String user, byte[] encodedthrows IOException
62     {
63 
64 //        File passwdFile = new File("qpid.passwd");
65 
66         PrintStream ps = new PrintStream(System.out);
67 
68         user += ":";
69         ps.write(user.getBytes("utf-8"));
70 
71         for (byte b : encoded)
72         {
73             ps.write(b);
74         }
75 
76         ps.println();
77 
78         ps.flush();
79         ps.close();
80     }
81 }