001/**
002 *
003 * Copyright 2019 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.packet.id;
018
019import org.jivesoftware.smack.util.StringUtils;
020
021public final class RandomStringStanzaIdSource  {
022
023    public static class Factory implements StanzaIdSourceFactory {
024
025        private static final int REQUIRED_MIN_LENGTH = 10;
026
027        private final int length;
028        private final boolean verySecure;
029
030        public static final Factory VERY_SECURE = new Factory(10, true);
031        public static final Factory MEDIUM_SECURE = new Factory(10, false);
032
033        public Factory(int length, boolean verySecure) {
034            if (length < REQUIRED_MIN_LENGTH) {
035                throw new IllegalArgumentException(
036                                "Insufficient length " + length + ", must be at least " + REQUIRED_MIN_LENGTH);
037            }
038            this.length = length;
039            this.verySecure = verySecure;
040        }
041
042        @Override
043        public StanzaIdSource constructStanzaIdSource() {
044            StanzaIdSource stanzaIdSource;
045            if (verySecure) {
046                stanzaIdSource = () -> StringUtils.randomString(length);
047            } else {
048                stanzaIdSource = () -> StringUtils.insecureRandomString(length);
049            }
050            return stanzaIdSource;
051        }
052
053    }
054}