/*
* EncryptUtils.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.lambdasoft.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @author TangLei <justinlei@gmail.com>
* @date 2008-11-28
*/
public class EncryptUtils {
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)
private byte[] buffer = new byte[64]; // input buffer
public static String digestHexStr;
private static byte[] digest = new byte[16];
public static String getMD5ofStr(String inbuf) {
EncryptUtils md5 = new EncryptUtils();
md5.md5Init();
md5.md5Update(inbuf.getBytes(), inbuf.length());
md5.md5Final();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
public EncryptUtils() {
md5Init();
}
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private void md5Update(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
}
private void md5Final() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
md5Update(PADDING, padLen);
// /* Append length (before padding) */
md5Update(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}
private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
private void md5Transform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, </s
分享到:
相关推荐
### Java 中将 UUID 存储为 Base64 字符串 #### 一、引言 在软件开发领域,尤其是在处理大规模分布式系统时,UUID(通用唯一标识符)因其全局唯一性而广泛应用于各种场景,例如作为数据库记录的主键、会话标识等。...
UUID生成工具类
java 生成8位UUID,解决UUID2太长的问题,欢迎下载。后续代码,陆续放出
本篇我们将深入探讨如何在不依赖JDK 5的新特性(如`java.util.UUID`类)的情况下,自行实现一个UUID生成器。 首先,我们需要理解UUID的基本结构。一个UUID由五部分组成:时间戳、节点ID、序列号、变种和版本。...
综合以上信息,这个压缩包的内容很可能是提供了一套C语言的UUID生成工具,它可能包括源代码、头文件、编译脚本等,可以帮助开发者在Windows环境下方便地生成UUID。对于需要在C程序中创建唯一标识的开发者来说,这是...
例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码...
java生成唯一索引,Long型,区别uuid
在Java编程中,UUID(Universally Unique Identifier)是一种用于生成全局唯一标识符的标准。一个UUID由32个16进制数字组成,通常分为五段,用短横线分隔,形如`8-4-4-4-12`的36个字符。在32位UUID中,我们实际上是...
require('node-uuid')> uuid.v1()'f033db70-29e8-11e5-9cde-5fb5e520454c'> uuid.v4()'3d694534-6ff4-426d-a4ec-bd4366f368e7'V1 是基于时间生成的 UUID,而V2 是基于随机数生成的。生成的 UUID 的第 13 位表示版本...
Java中的UUID生成和特定的ID生成库如Vesta-ID-Generator,为开发者提供了灵活和可靠的工具来解决这一问题。在实际应用中,选择合适的ID生成策略和库,需要考虑系统的规模、性能需求以及数据的唯一性和安全性。
此外,我们还可以使用 UUID 来实现一些安全机制,例如生成一个 token 来验证用户的身份。 Java 获取 UUID 和 UUID 校验是非常重要的操作,在实际应用中有着非常广泛的应用。通过本文,我们可以了解到 UUID 的基本...
GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复
UUID的生成算法有多种,如基于时间的、随机的、DCE安全的以及MD5和SHA-1哈希算法等。这种32位UUID生成工具可能采用了其中的一种或几种算法来确保生成的标识符在全球范围内是独一无二的。例如,基于时间的UUID会结合...
总之,UUID16是关于Java中生成全局唯一标识符的一种方式,它涉及到UUID的版本1生成机制,包含时间戳、节点ID和序列号,以确保在分布式环境中生成的标识符的唯一性。通过理解和使用UUID,我们可以有效地在各种场景下...
- **引入**: 自 Java 5 开始,Java 提供了 `java.util.UUID` 类来生成 UUID。 - **实例化**: ```java UUID uuid = UUID.randomUUID(); ``` ##### 实现代码示例 ```java package com.mytest; import java.util....
uuid生成,可生成16个字符的唯一码。使用方法,见main函数
标题中的"PB生成UUID"指的是在编程中使用Protocol Buffers(简称PB)生成Universally Unique Identifier(UUID)。UUID是一个128位的数字,通常表示为32个十六进制数字,用于唯一标识网络中的对象。它在分布式系统、...
有序UUID生成器 借鉴hibernate的UUID生成器 CustomVersionOneStrategy。
Java生成UUID使用的第三方包,生成UUID的第三方包,
下面,我们将深入探讨“javaScript动态随即生成UUID”的相关内容,包括UUID的基本概念、生成原理以及三种不同的生成方法。 ### UUID的基本概念 UUID,全称Universally Unique Identifier,即通用唯一识别码,是由...