| /* |
| * 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. |
| */ |
| package org.apache.dubbo.admin.registry.common.util; |
| |
| import com.alibaba.dubbo.common.io.Bytes; |
| |
| import java.security.MessageDigest; |
| import java.security.NoSuchAlgorithmException; |
| |
| public class Coder { |
| |
| private Coder() { |
| } |
| |
| public static String encodeHex(byte[] bytes) { |
| StringBuffer buffer = new StringBuffer(bytes.length * 2); |
| for (int i = 0; i < bytes.length; i++) { |
| if (((int) bytes[i] & 0xff) < 0x10) |
| buffer.append("0"); |
| buffer.append(Long.toString((int) bytes[i] & 0xff, 16)); |
| } |
| return buffer.toString(); |
| } |
| |
| public static String encodeMd5(String source) { |
| return encodeMd5(source.getBytes()); |
| } |
| |
| public static String encodeMd5(byte[] source) { |
| try { |
| return encodeHex(MessageDigest.getInstance("MD5").digest(source)); |
| } catch (NoSuchAlgorithmException e) { |
| throw new IllegalStateException(e.getMessage(), e); |
| } |
| } |
| |
| public static String encodeBase64(String source) { |
| return Bytes.bytes2base64(source.getBytes()); |
| } |
| |
| public static String decodeBase64(String source) { |
| return new String(Bytes.base642bytes(source)); |
| } |
| |
| } |