java digestutils.md5hex_linux下md5sum和DigestUtils.md5Hex的关系 博客分类: java

本文深入探讨了Linux系统下md5sum命令与Java中DigestUtils.md5Hex方法的实现原理。通过分析Linux核心utils包中的md5sum.c源码,展示了MD5校验和的计算过程,并对比了Java中对MD5的处理方式。内容涉及源码解读、文件读取模式以及不同系统下的兼容性处理。
摘要由CSDN通过智能技术生成

本文对linux下md5sum命令和java中DigestUtils.md5Hex进行比较,要了解各自的本质,就需要深入源码来了解各自运行机制。

1.linux下md5sum命令的源码

a.查看md5sum命令的位置

16567_0.png

b.查看md5sum在哪个package中

16567_1.png

c.从上可以知道md5sum命令在coreutils包中,下载coreutils包。查看md5sum的源码在md5sum.c文件中,内容如下:

Java代码

16567_2.png

/* Compute MD5 or SHA1 checksum of files or strings

Copyright (C) 1995-2002 Free Software Foundation, Inc.

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, 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.

You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software Foundation,

Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* Written by Ulrich Drepper . */

#include

#include

#include

#include

#include "system.h"

#include "md5.h"

#include "sha.h"

#include "checksum.h"

#include "getline.h"

#include "closeout.h"

#include "error.h"

/* The official name of this program (e.g., no `g' prefix). */

#define PROGRAM_NAME (algorithm == ALG_MD5 ? "md5sum" : "shasum")

#define AUTHORS N_ ("Ulrich Drepper and Scott Miller")

/* Most systems do not distinguish between external and internal

text representations. */

/* FIXME: This begs for an autoconf test. */

#if O_BINARY

# define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)

# define TEXT1TO1 "rb"

# define TEXTCNVT "r"

#else

# if defined VMS

# define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)

# define TEXT1TO1 "rb", "ctx=stm"

# define TEXTCNVT "r", "ctx=stm"

# else

# if UNIX || __UNIX__ || unix || __unix__ || _POSIX_VERSION

# define OPENOPTS(BINARY) "r"

# else

/* The following line is intended to evoke an error.

Using #error is not portable enough. */

"Cannot determine system type."

# endif

# endif

#endif

#define DIGEST_TYPE_STRING(Alg) ((Alg) == ALG_MD5 ? "MD5" : "SHA1")

#define DIGEST_STREAM(Alg) ((Alg) == ALG_MD5 ? md5_stream : sha_stream)

#define DIGEST_BITS(Alg) ((Alg) == ALG_MD5 ? 128 : 160)

#define DIGEST_HEX_BYTES(Alg) (DIGEST_BITS (Alg) / 4)

#define DIGEST_BIN_BYTES(Alg) (DIGEST_BITS (Alg) / 8)

#define MAX_DIGEST_BIN_BYTES MAX (DIGEST_BIN_BYTES (ALG_MD5),

DIGEST_BIN_BYTES (ALG_SHA1))

/* The minimum length of a valid digest line. This length does

not include any newline character at the end of a line. */

#define MIN_DIGEST_LINE_LENGTH(Alg)

(DIGEST_HEX_BYTES (Alg) /* length of hexadecimal message digest */

+ 2 /* blank and binary indicator */

+ 1 /* minimum filename length */ )

/* Nonzero if any of the files read were the standard input. */

static int have_read_stdin;

/* The minimum length of a valid checksum line for the selected algorithm. */

static size_t min_digest_line_length;

/* Set to the length of a digest hex string for the selected algorithm. */

static size_t digest_hex_bytes;

/* With --check, don't generate any output.

The exit code indicates success or failure. */

static int status_only = 0;

/* With --check, print a message to standard error warning about each

improperly formatted checksum line. */

static int warn = 0;

/* Declared and set via one of the wrapper .c files. */

/* int algorithm = ALG_UNSPECIFIED; */

/* The name this program was run with. */

char *program_name;

static const struct option long_options[] =

{

{ "binary", no_argument, 0, 'b' },

{ "check", no_argument, 0, 'c' },

{ "status", no_argument, 0, 2 },

{ "string", required_argument, 0, 1 },

{ "text", no_argument, 0, 't' },

{ "warn", no_argument, 0, 'w' },

{ GETOPT_HELP_OPTION_DECL },

{ GETOPT_VERSION_OPTION_DECL },

{ NULL, 0, NULL, 0 }

};

void

usage (int status)

{

if (status != 0)

fprintf (stderr, _("Try `%s --help' for more information.n"),

program_name);

else

{

printf (_("

Usage: %s [OPTION] [FILE]...n

or: %s [OPTION] --check [FILE]n

Print or check %s (%d-bit) checksums.n

With no FILE, or when FILE is -, read standard input.n

"),

program_name, program_name,

DIGEST_TYPE_STRING (algorithm),

DIGEST_BITS (algorithm));

printf (_("

n

-b, --binary read files in binary mode (default on DOS/Windows)n

-c, --check check %s sums against given listn

-t, --text read files in text mode (default)n

n

"),

DIGEST_TYPE_STRING (algorithm));

fputs (_("

The following two options are useful only when verifying checksums:n

--status don't output anything, status code shows successn

-w, --warn warn about improperly formated checksum linesn

n

"), stdout);

fputs (HELP_OPTION_DESCRIPTION, stdout);

fputs (VERSION_OPTION_DESCRIPTION, stdout);

printf (_("

n

The sums are computed as described in %s. When checking, the inputn

should be a former output of this program. The default mode is to printn

a line with checksum, a character indicating type (`*' for binary, ` ' forn

text), and name for each FILE.n"),

(algorithm == ALG_MD5 ? "RFC 1321" : "FIPS-180-1"));

printf (_("nReport bugs to

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值