Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lyf-beidou-cdp-api
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhoupeng
lyf-beidou-cdp-api
Commits
4625ea13
Commit
4625ea13
authored
Aug 28, 2023
by
pldjjj
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
46a795b6
9c33ad6a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
0 deletions
+88
-0
HmacSHA256Util.java
src/main/java/com/rome/order/domain/util/HmacSHA256Util.java
+88
-0
No files found.
src/main/java/com/rome/order/domain/util/HmacSHA256Util.java
0 → 100644
View file @
4625ea13
package
com
.
rome
.
order
.
domain
.
util
;
import
org.apache.commons.net.util.Base64
;
import
javax.crypto.Mac
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.util.Date
;
/**
* @Author: zhoupeng
* @createTime: 2023年08月28日 17:39:12
* @version: 1.0
* @Description:
* @copyright:
*/
public
class
HmacSHA256Util
{
/**
* HmacSHA256算法,返回的结果始终是32位
*
* @param key 加密的键,可以是任何数据
* @param content 待加密的内容
* @return 加密后的内容
* @throws Exception
*/
public
static
byte
[]
hmacSHA256
(
byte
[]
key
,
byte
[]
content
)
throws
Exception
{
Mac
hmacSha256
=
Mac
.
getInstance
(
"HmacSHA256"
);
hmacSha256
.
init
(
new
SecretKeySpec
(
key
,
0
,
key
.
length
,
"HmacSHA256"
));
byte
[]
hmacSha256Bytes
=
hmacSha256
.
doFinal
(
content
);
return
hmacSha256Bytes
;
}
/**
* 将加密后的字节数组转换成字符串
*
* @param b 字节数组
* @return 字符串
*/
public
static
String
byteArrayToHexString
(
byte
[]
b
)
{
StringBuilder
hs
=
new
StringBuilder
();
String
stmp
;
for
(
int
n
=
0
;
b
!=
null
&&
n
<
b
.
length
;
n
++)
{
stmp
=
Integer
.
toHexString
(
b
[
n
]
&
0XFF
);
if
(
stmp
.
length
()
==
1
)
hs
.
append
(
'0'
);
hs
.
append
(
stmp
);
}
return
hs
.
toString
().
toLowerCase
();
}
/**
* sha256_HMAC加密
*
* @param message 消息
* @param secret 秘钥
* @return 加密后字符串
*/
public
static
String
hmacSHA256
(
String
secret
,
String
message
)
throws
Exception
{
String
hash
=
""
;
Mac
hmacSha256
=
Mac
.
getInstance
(
"HmacSHA256"
);
SecretKeySpec
secret_key
=
new
SecretKeySpec
(
secret
.
getBytes
(),
"HmacSHA256"
);
hmacSha256
.
init
(
secret_key
);
byte
[]
bytes
=
hmacSha256
.
doFinal
(
message
.
getBytes
());
hash
=
byteArrayToHexString
(
bytes
);
return
hash
;
}
public
static
void
main
(
String
[]
args
)
{
String
enterpriseId
=
"10000001"
;
String
timestamp
=
DateUtils
.
convert
(
new
Date
(),
DateUtils
.
YYYYMMDDHHMMSS
);
// HMAC-SHA256(enterpriseId+ timestamp+去除节点间的空格及换行符的请求体)
String
message
=
enterpriseId
+
timestamp
;
System
.
out
.
println
(
"加密前 =========>"
+
message
);
String
str
=
""
;
try
{
str
=
HmacSHA256Util
.
hmacSHA256
(
enterpriseId
,
timestamp
);
String
encodeStr
=
Base64
.
encodeBase64String
(
str
.
getBytes
());
System
.
out
.
println
(
"加密后 =========>"
+
str
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Error HmacSHA256 ===========>"
+
e
.
getMessage
());
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment