分享:用四十种语言分别写一个MD5算法 之20 MATLAB语言MD5算法

本文分享了如何使用MATLAB语言实现MD5哈希算法,包括详细步骤和关键函数,如binStr2mat、inRange等。通过示例展示了如何对字符串'hello csdner'进行MD5加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享:用四十种语言分别写一个MD5算法 之20 MATLAB语言MD5算法

function digest = md5(message)
 
%% Helper Function Definitions
 
    %This function converts a binary representation of a number or vector
    %of numbers from a string to a row vector of 1's and 0's
    function matrix = binStr2mat(binStr)
        matrix = zeros(size(binStr));
        for n = (1:numel(binStr))
            matrix(n) = str2double(binStr(n));
        end
    end
 
    %This acts as a "lower <= x <= upper" operator
    function trueFalse = inRange(lowerBound,theValue,upperBound)
        trueFalse = (lowerBound <= theValue) && (theValue <= upperBound);
    end
 
    %This converts a decimal number into its 32-bit binary representation,
    %and I'm pretty sure it is little-endian
    function binaryRep = to32BitBin(decimal)
        binaryRep = binStr2mat(dec2bin(decimal,32));
    end
 
    %This converts a decimal number into its 64-bit binary representation,
    %and I'm pretty sure it is little-endian
    function binaryRep = to64BitBin(decimal)
        binaryRep = binStr2mat(dec2bin(decimal,64));
    end
 
    %This adds multiple binary numbers together modulo 2^32. The sum of these
    %numbers will rap around if the sum of two numbers is >= 2^32
    function result = addBinary(varargin)      
        result = 0;        
        for l = (1:numel(varargin))
            temp = num2str(varargin{l});
            temp(temp == ' ') = [];
            result = mod(result + bin2dec(temp),2^32);
        end
        result = to32BitBin(result);
    end
 
%% MD5 Hash Algorithm
 
%Define constants
    %r is the bit-shift amount for each round
    r =[7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,...
   &
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值