MessageAuthenticator

MessageAuthenticator

https://coova.github.io/JRadius/
https://coova.github.io/JRadius/

import org.tinyradius.packet.RadiusPacket;
import org.tinyradius.util.RadiusUtil;
import java.nio.charset.StandardCharsets;public class RadiusAuthUtils {/*** 生成 RADIUS 報文的 Message-Authenticator 值* @param sharedSecret 共享密鑰* @param requestAuthenticator 請求認證器(16 字節隨機數)* @param packetBytes 完整的 RADIUS 報文字節數據(包含報文頭和屬性)* @return Message-Authenticator 的字節數組(16 字節)*/public static byte[] generateMessageAuthenticator(String sharedSecret, byte[] requestAuthenticator, byte[] packetBytes) {try {// 1. 使用 HMAC-MD5 計算哈希byte[] hmacMd5 = RadiusUtil.getMd5Digest(sharedSecret.getBytes(StandardCharsets.UTF_8), packetBytes);// 2. 提取前 16 字節作為 Message-Authenticatorbyte[] messageAuthenticator = new byte[16];System.arraycopy(hmacMd5, 0, messageAuthenticator, 0, 16);return messageAuthenticator;} catch (Exception e) {throw new RuntimeException("生成 Message-Authenticator 失敗", e);}}
}
public static byte[] generateMessageAuthenticator(byte[] sharedSecret, RADIUS packet) {try {byte[] messageBytes = packet.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(sharedSecret, "HmacMD5");Mac mac = Mac.getInstance("HmacMD5"); mac.init(secretKeySpec); byte[] macValue = mac.doFinal(messageBytes); Attribute macAttribute = new MessageAuthenticator(macValue);List<Attribute> attributes = packet.getAttributes(); attributes.add(macAttribute); return macValue;} catch (NoSuchAlgorithmException | InvalidKeyException e) {throw new RuntimeException("Failed to generate Message-Authenticator", e);}
}

jradius-1.1.5

package net.jradius.util;import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.io.IOException;
import java.nio.ByteBuffer;import net.jradius.packet.RadiusFormat;
import net.jradius.packet.RadiusPacket;
import net.jradius.packet.attribute.AttributeDictionary;
import net.jradius.packet.attribute.AttributeFactory;
import net.jradius.packet.attribute.RadiusAttribute;public class MessageAuthenticator 
{private static final RadiusFormat format = RadiusFormat.getInstance();public static void generateRequestMessageAuthenticator(RadiusPacket request, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException{byte[] hash = new byte[16];ByteBuffer buffer = ByteBuffer.allocate(4096);request.overwriteAttribute(AttributeFactory.newAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR, hash, request.isRecyclable()));format.packPacket(request, sharedSecret, buffer, true);System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);}public static void generateResponseMessageAuthenticator(RadiusPacket request, RadiusPacket reply, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException{byte[] hash = new byte[16];byte[] requestAuth = request.getAuthenticator();byte[] replyAuth = reply.getAuthenticator();ByteBuffer buffer = ByteBuffer.allocate(4096);reply.setAuthenticator(requestAuth);reply.overwriteAttribute(AttributeFactory.newAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR, hash, reply.isRecyclable()));format.packPacket(reply, sharedSecret, buffer, true);System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);reply.setAuthenticator(replyAuth);}public static Boolean verifyRequest(RadiusPacket request, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException{byte[] hash = new byte[16];ByteBuffer buffer = ByteBuffer.allocate(4096);RadiusAttribute attr = request.findAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR);if (attr == null) return null;byte[] pval = attr.getValue().getBytes();attr.setValue(hash);format.packPacket(request, sharedSecret, buffer, true);System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);attr.setValue(pval);return new Boolean(Arrays.equals(pval, hash));}public static Boolean verifyReply(byte[] requestAuth, RadiusPacket reply, String sharedSecret) throws IOException, InvalidKeyException, NoSuchAlgorithmException{byte[] replyAuth = reply.getAuthenticator();byte[] hash = new byte[16];ByteBuffer buffer = ByteBuffer.allocate(4096);RadiusAttribute attr = reply.findAttribute(AttributeDictionary.MESSAGE_AUTHENTICATOR);if (attr == null) return null;byte[] pval = attr.getValue().getBytes();attr.setValue(hash);reply.setAuthenticator(requestAuth);format.packPacket(reply, sharedSecret, buffer, true);System.arraycopy(MD5.hmac_md5(buffer.array(), 0, buffer.position(), sharedSecret.getBytes()), 0, hash, 0, 16);reply.setAuthenticator(replyAuth);return new Boolean(Arrays.equals(pval, hash));}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/70540.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/70540.shtml
英文地址,請注明出處:http://en.pswp.cn/web/70540.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Spring Boot嵌入式服務器深度解析:從配置到調優的全方位指南

文章目錄 引言一、嵌入式服務器核心原理1.1 架構設計特點1.2 主流服務器對比 二、嵌入式服務器配置實戰2.1 基礎配置模板2.2 HTTPS安全配置 三、高級調優策略3.1 線程池優化&#xff08;Tomcat示例&#xff09;3.2 響應壓縮配置3.3 訪問日志配置 四、服務器切換實戰4.1 切換至U…

基于CentOS7安裝kubesphere和Kubernetes并接入外部ES收集日志

一、修改所有節點主機名 主節點就修改成master hostnamectl set-hostname master 然后輸入bash刷新當前主機名 工作節點1就修改成node1 hostnamectl set-hostname node1 然后輸入bash刷新當前主機名 二、全部節點安裝依賴并同步時間 yum -y install socat conntrack ebta…

探索與Cursor協作創建一個完整的前后端分離的項目的最佳實踐

探索與Cursor協作創建一個完整的前后端分離的項目的最佳實踐 Cursor簡介 Cursor在目前代表了AI編程技術的頂峰。在一定程度上可以說是當今AI時代的最強生產力代表。為此,不惜重金開了年費會員來緊跟時代步伐。當然cline、roo code、trae等開源或者免費產品也在緊追不舍。 C…

支持向量機(SVM)在 NLP 中的使用場景

支持向量機(Support Vector Machine, SVM)是一種強大的監督學習算法,廣泛應用于分類任務中。由于其出色的分類性能和高效的計算特點,SVM 已經成為自然語言處理(NLP)領域中的一種經典模型。SVM 在 NLP 中的應用非常廣泛,尤其在文本分類任務中,表現出色。 本文將探討 SV…

nodejs:vue 3 + vite 作為前端,將 html 填入<iframe>,在線查詢英漢詞典

向 doubao.com/chat/ 提問&#xff1a; node.js js-mdict 作為后端&#xff0c;vue 3 vite 作為前端&#xff0c;編寫在線查詢英漢詞典 后端部分&#xff08;express js-mdict &#xff09; 詳見上一篇&#xff1a;nodejs&#xff1a;express js-mdict 作為后端&#xff…

Jenkins 部署在 Mac 并在局域網內通過 ip 訪問

Jenkins 部署在 Mac 并在局域網內通過 ip 訪問 一、修改配置文件 打開文件 ~/Library/LaunchAgents/homebrew.mxcl.jenkins.plist 打開文件 /usr/local/opt/jenkins/homebrew.mxcl.jenkins.plist 兩個文件目錄不同&#xff0c;內容一樣 <?xml version"1.0" e…

2通道12bit 10G USB高速示波器采集卡

概述 USB高速示波器采集卡 2通道&#xff0c;12位&#xff0c;10GSa/s 采樣率 DC~2.5GHz 帶寬 USB高速示波器采集卡是一款高速12bit多通道USB數字化儀它具有2通道10GSa/s采樣率&#xff0c;模擬前端帶寬從DC到2.5GHz&#xff0c;板載32GB DDR4存儲&#xff0c;使其能夠滿足長…

Python|OpenCV-實現人物眨眼檢測(21)

前言 本文是該專欄的第23篇,后面將持續分享OpenCV計算機視覺的干貨知識,記得關注。 通過OpenCV庫來實現人物的眨眼檢測,首先是需要了解眨眼檢測的基本原理。一般來說,是需要通過檢測眼睛的狀態,比如眼睛是否閉合來判斷是否眨眼。對此,如果基于OpenCV,通過Python如何去實…

Qt | Excel創建、打開、讀寫、另存和關閉

01 如何在Qt中使用QXlsx庫進行Excel文件的讀寫操作,包括創建新Excel、寫入數據、讀取數據以及文件保存和釋放資源。通過實例展示了如何加載庫、編寫.h和.cpp文件,并演示了使用單元格引用和行列號進行數據操作的方法。 QXlsx是一個可以讀寫Excel文件的庫。不依賴office以及…

AMBA-CHI協議詳解(十九)

文章目錄 4.6 Silent cache state transitions4.7 Cache state transitions at a Requester4.7.1 Read request transactions4.7.2 Dataless request transactions4.7.3 Write request transactions4.7.4 Atomic transactions4.7.5 Other request transactions4.6 Silent cache…

常見的“鎖”有哪些?

悲觀鎖 悲觀鎖認為在并發環境中&#xff0c;數據隨時可能被其他線程修改&#xff0c;因此在訪問數據之前會先加鎖&#xff0c;以防止其他線程對數據進行修改。常見的悲觀鎖實現有&#xff1a; 1.互斥鎖 原理&#xff1a;互斥鎖是一種最基本的鎖類型&#xff0c;同一時間只允…

深入理解 Python 作用域:從基礎到高級應用

在 Python 編程中&#xff0c;作用域是一個至關重要的概念&#xff0c;它決定了變量和函數的可見性與生命周期。正確理解和運用作用域規則&#xff0c;對于編寫結構清晰、易于維護的代碼起著關鍵作用。無論是簡單的腳本還是復雜的大型項目&#xff0c;作用域都貫穿其中&#xf…

ubuntu磁盤清理垃圾文件

大頭文件排查 #先查看是否是內存滿了&#xff0c;USER 很高即是滿了 du -f#抓大頭思想&#xff0c;優先刪除大文件#查看文件目錄 內存占用量并排序&#xff0c;不斷文件遞歸下去 du --max-depth1 -h /home/ -h | sort du --max-depth1 -h /home/big/ -h | sort 緩存文件清理…

ctf網絡安全題庫 ctf網絡安全大賽答案

此題解僅為部分題解&#xff0c;包括&#xff1a; 【RE】&#xff1a;①Reverse_Checkin ②SimplePE ③EzGame 【Web】①f12 ②ezrunner 【Crypto】①MD5 ②password ③看我回旋踢 ④摩絲 【Misc】①爆爆爆爆 ②凱撒大帝的三個秘密 ③你才是職業選手 一、 Re ① Reverse Chec…

VSCode集成deepseek使用介紹(Visual Studio Code)

VSCode集成deepseek使用介紹&#xff08;Visual Studio Code&#xff09; 1. 簡介 隨著AI輔助編程工具的快速發展&#xff0c;VSCode作為一款輕量級、高度可擴展的代碼編輯器&#xff0c;已成為開發者首選的工具之一。DeepSeek作為AI模型&#xff0c;結合Roo Code插件&#x…

git 常用功能

以下是 Git 的常用功能及其命令&#xff1a; 初始化倉庫 git init在當前目錄初始化一個新的 Git 倉庫。 克隆倉庫 git clone <倉庫地址>將遠程倉庫克隆到本地。 查看狀態 git status查看工作區和暫存區的狀態。 添加文件到暫存區 git add <文件名>將文件添…

Unity 腳本控制3D人物模型的BlendShape

有些3D角色模型帶有BlendShape面部控制, 在Unity中可以通過接口訪問并操作其參數可以表現不同的面部表情 在Unity中選中角色模型的指定部位,這個是由模型師定義的,不固定.但肯定是在面部建模上. 點選之后在檢查器可以看到對應的BlendShapes設定項出現在SkinedMeshRenderer組件…

vscode設置終端復制快捷鍵(有坑!!!)

vscode的編輯頁面和終端的復制粘貼快捷鍵是不一樣的。 vscode的終端復制快捷鍵為ctrlshiftC&#xff0c;當然&#xff0c;自己可以自定義設置 vscode設置終端復制快捷鍵&#xff08;有坑&#xff01;&#xff01;&#xff01;&#xff09;_vs code 不能復制-CSDN博客文章瀏覽…

Ansible 學習筆記

這里寫自定義目錄標題 基本架構文件結構安裝查看版本 Ansible 配置相關文件主機清單寫法 基本架構 Ansible 是基于Python實現的&#xff0c;默認使用22端口&#xff0c; 文件結構 安裝 查看用什么語言寫的用一下命令 查看版本 Ansible 配置相關文件 主機清單寫法

0083.基于springboot+uni-app的社區車位租賃系統小程序+論文

一、系統說明 基于springbootuni-app的社區車位租賃系統小程序,系統功能齊全, 代碼簡潔易懂&#xff0c;適合小白學編程。 現如今&#xff0c;信息種類變得越來越多&#xff0c;信息的容量也變得越來越大&#xff0c;這就是信息時代的標志。近些年&#xff0c;計算機科學發展…