001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved 005 * 006 * The contents of this file are subject to the terms 007 * of the Common Development and Distribution License 008 * (the License). You may not use this file except in 009 * compliance with the License. 010 * 011 * You can obtain a copy of the License at 012 * https://opensso.dev.java.net/public/CDDLv1.0.html or 013 * opensso/legal/CDDLv1.0.txt 014 * See the License for the specific language governing 015 * permission and limitations under the License. 016 * 017 * When distributing Covered Code, include this CDDL 018 * Header Notice in each file and include the License file 019 * at opensso/legal/CDDLv1.0.txt. 020 * If applicable, add the following below the CDDL Header, 021 * with the fields enclosed by brackets [] replaced by 022 * your own identifying information: 023 * "Portions Copyrighted [year] [name of copyright owner]" 024 * 025 * $Id: HexToBase64.java,v 1.2 2008/06/25 05:47:33 qcheng Exp $ 026 * 027 */ 028 029package com.sun.identity.saml.common; 030 031import com.sun.identity.shared.encode.Base64; 032 033/** 034 * This class <code>HexToBase64</code> is used to convert Hex encoded 035 * SAML source ID to Base64 encoded ID. 036 * 037 * @supported.all.api 038 */ 039public class HexToBase64 { 040 041 /** 042 * Returns Base64 encoded source ID based on the input Hex source ID. 043 * <br>Usage: java com.sun.identity.saml.common.HexToBase64 <Hex_encoded_id><br> 044 * This method will print out Base64 encoded source ID to the standard 045 * output. 046 * 047 * @param args Hex encoded source ID. 048 */ 049 public static void main(String args[]) throws Exception { 050 051 if (args.length != 1) { 052 System.out.println("Usage: java HexToBase64 <Hex_encoded_id>"); 053 return; 054 } 055 Base64 encoder = new Base64(); 056 String hexString = args[0]; 057 int read = hexString.length(); 058 byte[] byteArray = new byte[read/2]; 059 for (int i=0, j=0; i < read; i++, j++) { 060 String part = hexString.substring(i,i+2); 061 byteArray[j] = 062 new Short(Integer.toString(Integer.parseInt(part,16))). 063 byteValue(); 064 i++; 065 } 066 try { 067 String encodedID = encoder.encode(byteArray).trim(); 068 System.out.println(encodedID); 069 } catch (Exception e) { 070 e.printStackTrace(); 071 } 072 } 073}