| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>H64.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Jar Bundle</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.codec</a> > <span class="el_source">H64.java</span></div><h1>H64.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| /* |
| * The apr_md5_encode() routine in the APR project's apr_md5.c file uses much |
| * code obtained from the FreeBSD 3.0 MD5 crypt() function, which is licenced |
| * as follows: |
| * ---------------------------------------------------------------------------- |
| * "THE BEER-WARE LICENSE" (Revision 42): |
| * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you |
| * can do whatever you want with this stuff. If we meet some day, and you think |
| * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
| * ---------------------------------------------------------------------------- |
| */ |
| package org.apache.shiro.codec; |
| |
| import java.io.IOException; |
| |
| /** |
| * Codec for <a href="http://en.wikipedia.org/wiki/Crypt_(Unix)">Unix Crypt</a>-style encoding. While similar to |
| * Base64, it is not compatible with Base64. |
| * <p/> |
| * This implementation is based on encoding algorithms found in the Apache Portable Runtime library's |
| * <a href="http://svn.apache.org/viewvc/apr/apr/trunk/crypto/apr_md5.c?revision=HEAD&view=markup">apr_md5.c</a> |
| * implementation for its {@code crypt}-style support. The APR team in turn received inspiration for its encoding |
| * implementation based on FreeBSD 3.0's {@code /usr/src/lib/libcrypt/crypt.c} implementation. The |
| * accompanying license headers have been retained at the top of this source file. |
| * <p/> |
| * This file and all that it contains is ASL 2.0 compatible. |
| * |
| * @since 1.2 |
| */ |
| <span class="nc" id="L48">public class H64 {</span> |
| |
| <span class="fc" id="L50"> private static final char[] itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();</span> |
| |
| private static short toShort(byte b) { |
| <span class="fc" id="L53"> return (short) (b & 0xff);</span> |
| } |
| |
| private static int toInt(byte[] bytes, int offset, int numBytes) { |
| <span class="pc bpc" id="L57" title="2 of 4 branches missed."> if (numBytes < 1 || numBytes > 4) {</span> |
| <span class="nc" id="L58"> throw new IllegalArgumentException("numBytes must be between 1 and 4.");</span> |
| } |
| <span class="fc" id="L60"> int val = toShort(bytes[offset]); //1st byte</span> |
| <span class="fc bfc" id="L61" title="All 2 branches covered."> for (int i = 1; i < numBytes; i++) { //any remaining bytes:</span> |
| <span class="fc" id="L62"> short s = toShort(bytes[offset + i]);</span> |
| <span class="pc bpc" id="L63" title="2 of 4 branches missed."> switch (i) {</span> |
| <span class="fc" id="L64"> case 1: val |= s << 8; break;</span> |
| <span class="fc" id="L65"> case 2: val |= s << 16; break;</span> |
| <span class="nc" id="L66"> case 3: val |= s << 24; break;</span> |
| } |
| } |
| <span class="fc" id="L69"> return val;</span> |
| } |
| |
| /** |
| * Appends the specified character into the buffer, rethrowing any encountered |
| * {@link IOException} as an {@link IllegalStateException} (since this method is used for internal |
| * implementation needs and we only ever use StringBuilders, we should never encounter an IOException). |
| * |
| * @param buf the buffer to append to |
| * @param c the character to append. |
| */ |
| private static void append(Appendable buf, char c) { |
| try { |
| <span class="fc" id="L82"> buf.append(c);</span> |
| <span class="nc" id="L83"> } catch (IOException e) {</span> |
| <span class="nc" id="L84"> throw new IllegalStateException("Unable to append character to internal buffer.", e);</span> |
| <span class="fc" id="L85"> }</span> |
| <span class="fc" id="L86"> }</span> |
| |
| /** |
| * Encodes the specified integer to {@code numChars} H64-compatible characters and appends them into {@code buf}. |
| * |
| * @param value the integer to encode to H64-compatible characters |
| * @param buf the output buffer |
| * @param numChars the number of characters the value should be converted to. 3, 2 or 1. |
| */ |
| private static void encodeAndAppend(int value, Appendable buf, int numChars) { |
| <span class="fc bfc" id="L96" title="All 2 branches covered."> for (int i = 0; i < numChars; i++) {</span> |
| <span class="fc" id="L97"> append(buf, itoa64[value & 0x3f]);</span> |
| <span class="fc" id="L98"> value >>= 6;</span> |
| } |
| <span class="fc" id="L100"> }</span> |
| |
| /** |
| * Encodes the specified bytes to an {@code H64}-encoded String. |
| * |
| * @param bytes |
| * @return |
| */ |
| public static String encodeToString(byte[] bytes) { |
| <span class="pc bpc" id="L109" title="2 of 4 branches missed."> if (bytes == null || bytes.length == 0) return null;</span> |
| |
| <span class="fc" id="L111"> StringBuilder buf = new StringBuilder();</span> |
| |
| <span class="fc" id="L113"> int length = bytes.length;</span> |
| <span class="fc" id="L114"> int remainder = length % 3;</span> |
| <span class="fc" id="L115"> int i = 0; //starting byte</span> |
| <span class="fc" id="L116"> int last3ByteIndex = length - remainder; //last byte whose index is a multiple of 3</span> |
| |
| <span class="fc bfc" id="L118" title="All 2 branches covered."> for(; i < last3ByteIndex; i += 3) {</span> |
| <span class="fc" id="L119"> int twentyFourBit = toInt(bytes, i, 3);</span> |
| <span class="fc" id="L120"> encodeAndAppend(twentyFourBit, buf, 4);</span> |
| } |
| <span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (remainder > 0) {</span> |
| //one or two bytes that we still need to encode: |
| <span class="nc" id="L124"> int a = toInt(bytes, i, remainder);</span> |
| <span class="nc" id="L125"> encodeAndAppend(a, buf, remainder + 1);</span> |
| } |
| <span class="fc" id="L127"> return buf.toString();</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html> |