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.mzph.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;计算机科学发展…