001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.uuid;
016    
017    import com.liferay.portal.kernel.security.SecureRandomUtil;
018    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.uuid.PortalUUID;
023    
024    import java.util.UUID;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    @DoPrivileged
030    public class PortalUUIDImpl implements PortalUUID {
031    
032            @Override
033            public String fromJsSafeUuid(String jsSafeUuid) {
034                    return StringUtil.replace(
035                            jsSafeUuid, StringPool.DOUBLE_UNDERLINE, StringPool.DASH);
036            }
037    
038            @Override
039            public String generate() {
040                    UUID uuid = new UUID(
041                            SecureRandomUtil.nextLong(), SecureRandomUtil.nextLong());
042    
043                    return uuid.toString();
044            }
045    
046            @Override
047            public String generate(byte[] bytes) {
048                    return UUID.nameUUIDFromBytes(bytes).toString();
049            }
050    
051            @Override
052            public String toJsSafeUuid(String uuid) {
053                    return StringUtil.replace(
054                            uuid, CharPool.DASH, StringPool.DOUBLE_UNDERLINE);
055            }
056    
057    }