public String getDigest(String password) {
    if (Validator.isNull(getScreenName())) {
      throw new IllegalStateException("Screen name cannot be null");
    } else if (Validator.isNull(getEmailAddress())) {
      throw new IllegalStateException("Email address cannot be null");
    }

    StringBundler sb = new StringBundler(5);

    String digest1 =
        DigesterUtil.digestHex(Digester.MD5, getEmailAddress(), Portal.PORTAL_REALM, password);

    sb.append(digest1);
    sb.append(StringPool.COMMA);

    String digest2 =
        DigesterUtil.digestHex(Digester.MD5, getScreenName(), Portal.PORTAL_REALM, password);

    sb.append(digest2);
    sb.append(StringPool.COMMA);

    String digest3 =
        DigesterUtil.digestHex(
            Digester.MD5, String.valueOf(getUserId()), Portal.PORTAL_REALM, password);

    sb.append(digest3);

    return sb.toString();
  }
Example #2
0
  @Test
  public void testNowikiWithFormat() throws Exception {
    DigesterUtil digesterUtil = new DigesterUtil();

    Digester digester = Mockito.mock(Digester.class);

    Mockito.when(digester.digest(Mockito.anyString())).thenReturn(RandomTestUtil.randomString());

    digesterUtil.setDigester(digester);

    String content =
        "previous line\n<nowiki>\nmonospace\n''second'' " + "line\n</nowiki>\nnext line";

    String expected =
        MediaWikiToCreoleTranslator.TABLE_OF_CONTENTS
            + "previous line\n{{{{\nmonospace\n''second'' line\n}}}}\nnext"
            + " line";

    String actual = _mediaWikiToCreoleTranslator.translate(content);

    Assert.assertEquals(expected, actual);
  }
Example #3
0
  protected static String encodePassword(
      String algorithm, String clearTextPassword, byte[] saltBytes) throws PwdEncryptorException {

    try {
      if (algorithm.equals(TYPE_BCRYPT)) {
        String salt = new String(saltBytes);

        return BCrypt.hashpw(clearTextPassword, salt);
      } else if (algorithm.equals(TYPE_CRYPT) || algorithm.equals(TYPE_UFC_CRYPT)) {

        return Crypt.crypt(saltBytes, clearTextPassword.getBytes(Digester.ENCODING));
      } else if (algorithm.equals(TYPE_SSHA)) {
        byte[] clearTextPasswordBytes = clearTextPassword.getBytes(Digester.ENCODING);

        // Create a byte array of salt bytes appended to password bytes

        byte[] pwdPlusSalt = new byte[clearTextPasswordBytes.length + saltBytes.length];

        System.arraycopy(clearTextPasswordBytes, 0, pwdPlusSalt, 0, clearTextPasswordBytes.length);

        System.arraycopy(
            saltBytes, 0, pwdPlusSalt, clearTextPasswordBytes.length, saltBytes.length);

        // Digest byte array

        MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");

        byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);

        // Appends salt bytes to the SHA-1 digest.

        byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length];

        System.arraycopy(pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length);

        System.arraycopy(saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length);

        // Base64 encode and format string

        return Base64.encode(digestPlusSalt);
      } else {
        return DigesterUtil.digest(algorithm, clearTextPassword);
      }
    } catch (NoSuchAlgorithmException nsae) {
      throw new PwdEncryptorException(nsae.getMessage());
    } catch (UnsupportedEncodingException uee) {
      throw new PwdEncryptorException(uee.getMessage());
    }
  }
  @Before
  public void setUp() {
    DigesterUtil digesterUtil = new DigesterUtil();

    digesterUtil.setDigester(new DigesterImpl());

    PasswordEncryptorUtil passwordEncryptorUtil = new PasswordEncryptorUtil();

    CompositePasswordEncryptor compositePasswordEncryptor = new CompositePasswordEncryptor();

    compositePasswordEncryptor.setDefaultPasswordEncryptor(new DefaultPasswordEncryptor());

    List<PasswordEncryptor> passwordEncryptors = new ArrayList<>();

    passwordEncryptors.add(new BCryptPasswordEncryptor());
    passwordEncryptors.add(new CryptPasswordEncryptor());
    passwordEncryptors.add(new NullPasswordEncryptor());
    passwordEncryptors.add(new PBKDF2PasswordEncryptor());
    passwordEncryptors.add(new SSHAPasswordEncryptor());

    compositePasswordEncryptor.setPasswordEncryptors(passwordEncryptors);

    passwordEncryptorUtil.setPasswordEncryptor(compositePasswordEncryptor);
  }
  protected String getUID(long companyId, String languageId, String word, String... parameters) {

    StringBundler uidSB = new StringBundler();

    uidSB.append(String.valueOf(companyId));
    uidSB.append(StringPool.UNDERLINE);
    uidSB.append(PortletKeys.SEARCH);
    uidSB.append(_PORTLET_SEPARATOR);

    int length = 4;

    if (parameters != null) {
      length += parameters.length;
    }

    try {
      CharsetEncoder charsetEncoder = CharsetEncoderUtil.getCharsetEncoder(StringPool.UTF8);

      StringBundler keySB = new StringBundler(length);

      keySB.append(languageId);
      keySB.append(StringPool.UNDERLINE);
      keySB.append(word);
      keySB.append(StringPool.UNDERLINE);

      keySB.append(StringUtil.toLowerCase(word));

      if (parameters != null) {
        for (String parameter : parameters) {
          keySB.append(parameter);
          keySB.append(StringPool.UNDERLINE);
        }
      }

      String key = keySB.toString();

      byte[] bytes =
          DigesterUtil.digestRaw(Digester.MD5, charsetEncoder.encode(CharBuffer.wrap(key)));

      uidSB.append(Base64.encode(bytes));
    } catch (Exception e) {
      throw new IllegalStateException(e);
    }

    return uidSB.toString();
  }
Example #6
0
  @Override
  protected String doEncrypt(String algorithm, String plainTextPassword, String encryptedPassword) {

    return DigesterUtil.digest(algorithm, plainTextPassword);
  }
Example #7
0
 public static String getChecksum(InputStream inputStream) {
   return DigesterUtil.digestBase64(Digester.SHA_1, inputStream);
 }