001/** 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 2006 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: SAMLSiteID.java,v 1.2 2008/06/25 05:47:35 qcheng Exp $ 026 * 027 */ 028 029 030package com.sun.identity.saml.common; 031 032import java.util.Random; 033import com.sun.identity.shared.encode.Base64; 034 035import java.security.MessageDigest; 036 037/** 038 * This class is used to generate SAML Site ID. 039 * 040 * @supported.all.api 041 */ 042public class SAMLSiteID { 043 044 private static Random random = new Random(); 045 046 private SAMLSiteID() { 047 } 048 049 /** 050 * Returns an ID String with length of 051 * <code>SAMLConstants.ID_LENGTH</code>. 052 * 053 * @return ID String or null if it fails. 054 */ 055 public static String generateID() { 056 if (random == null) { 057 return null; 058 } 059 byte bytes[] = new byte[SAMLConstants.ID_LENGTH]; 060 random.nextBytes(bytes); 061 String encodedID = null; 062 try { 063 encodedID = Base64.encode(bytes).trim(); 064 } catch (Exception e) { 065 e.printStackTrace(); 066 } 067 068 return encodedID; 069 } 070 071 /** 072 * Returns SAML site ID based on <code>siteURL</code>, this will return a 073 * <code>Base64</code> encoded <code>SHA-1</code> digest. 074 * 075 * @param siteURL site URL for example: 076 * <code>http://host.sun.com:58080</code>. 077 * @return Base64 encoded site ID 078 */ 079 public static String generateSourceID(String siteURL) { 080 if ((siteURL == null) || (siteURL.length() == 0)) { 081 return null; 082 } 083 MessageDigest md = null; 084 try { 085 md = MessageDigest.getInstance("SHA"); 086 } catch (Exception e) { 087 e.printStackTrace(); 088 return null; 089 } 090 char chars[] = siteURL.toCharArray(); 091 byte bytes[] = new byte[chars.length]; 092 for (int i = 0; i < chars.length; i++) { 093 bytes[i] = (byte) chars[i]; 094 } 095 md.update(bytes); 096 byte byteResult[] = md.digest(); 097 String result = null; 098 try { 099 result = Base64.encode(byteResult); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 104 return result; 105 } 106 107 /** 108 * Obtains site ID based on the host name. 109 * This method will print out site ID to the standard output. 110 * 111 * @param args host name 112 */ 113 public static void main(String[] args) { 114 if (args.length != 1) { 115 System.out.println("usage : java SAMLSiteID <host_name>"); 116 return; 117 } 118 119 System.out.println(generateSourceID(args[0])); 120 } 121}