"함수 hash hmac()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 12개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[category: 해시]]
[[category: 해시]]
{{DISPLAYTITLE:함수 hash_hmac()}}
;hash_hmac()
;hash_hmac()
==Java==
[[분류: Java]]
{{참고|자바 hashHmacSha1()}}
<syntaxhighlight lang='java'>
public static String hashHmacSha1(String value, String key) {
try {
byte[] hexBytes = new Hex().encode(hashHmacSha1Raw(value,key));
return new String(hexBytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static byte[] hashHmacSha1Raw(String value, String key) {
try {
byte[] keyBytes = key.getBytes();         
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
return mac.doFinal(value.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
System.out.println( hashHmacSha1( "hello world", "secret" ) );
// 03376ee7ad7bbfceee98660439a4d8b125122a5a
System.out.println( new String(hashHmacSha1Raw( "hello world", "secret" )) );
// �7n�{���f�9�ر%�*Z
}
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
{{참고|PHP hash_hmac()}}
<syntaxhighlight lang='php'>
echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
// b8e7ae12510bdfb1812e463a7f086122cf37e4f7
# b8e7ae12510bdfb1812e463a7f086122cf37e4f7
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
echo hash_hmac( 'sha1', 'hello world', 'secret' );
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
</syntaxhighlight>
<syntaxhighlight lang='php'>
$h = hash_hmac( 'sha1', 'hello world', 'secret', true );
$h = hash_hmac( 'sha1', 'hello world', 'secret', true );
echo base64_encode( $h );
echo base64_encode( $h );
# Azdu5617v87umGYEOaTYsSUSKlo=
# Azdu5617v87umGYEOaTYsSUSKlo=
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python'>
import hmac
import hmac
import base64
import base64
22번째 줄: 59번째 줄:
print( base64.b64encode( h ) )
print( base64.b64encode( h ) )
# sGXfsWEMawIp9we7B/iuuA==
# sGXfsWEMawIp9we7B/iuuA==
</source>
</syntaxhighlight>
<source lang='Python'>
<syntaxhighlight lang='Python'>
import hmac
import base64
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).hexdigest()
print( h )
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
</syntaxhighlight>
<syntaxhighlight lang='Python'>
import hmac
import hmac
import base64
import base64
import hashlib
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).digest()
print( base64.b64encode( h ) )
# Azdu5617v87umGYEOaTYsSUSKlo=
</syntaxhighlight>
==Perl==
[[분류: Perl]]
<syntaxhighlight lang='perl'>
use Digest::SHA qw(hmac_sha1_base64);
my $digest = hmac_sha1_base64("hello world", "secret");


secret = 'secret'
while (length($digest) % 4) {
message = 'hello world'
    $digest .= '=';
digestmod = hashlib.sha1
}


h = hmac.new(secret, message, digestmod).digest()
print $digest . "\n";
print( base64.b64encode( h ) )
# Azdu5617v87umGYEOaTYsSUSKlo=
# Azdu5617v87umGYEOaTYsSUSKlo=
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[HMAC]]
*[[HMAC]]

2020년 11월 2일 (월) 02:32 기준 최신판


hash_hmac()

1 Java[ | ]

public static String hashHmacSha1(String value, String key) {
	try {
		byte[] hexBytes = new Hex().encode(hashHmacSha1Raw(value,key));
		return new String(hexBytes, "UTF-8");
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
public static byte[] hashHmacSha1Raw(String value, String key) {
	try {
		byte[] keyBytes = key.getBytes();           
		SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
		Mac mac = Mac.getInstance("HmacSHA1");
		mac.init(signingKey);
		return mac.doFinal(value.getBytes());
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
public static void main(String[] args) {
	System.out.println( hashHmacSha1( "hello world", "secret" ) );
	// 03376ee7ad7bbfceee98660439a4d8b125122a5a
	System.out.println( new String(hashHmacSha1Raw( "hello world", "secret" )) );
	// �7n�{���f�9�ر%�*Z
}

2 PHP[ | ]

echo hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret');
# b8e7ae12510bdfb1812e463a7f086122cf37e4f7
echo hash_hmac( 'sha1', 'hello world', 'secret' );
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
$h = hash_hmac( 'sha1', 'hello world', 'secret', true );
echo base64_encode( $h );
# Azdu5617v87umGYEOaTYsSUSKlo=

3 Python[ | ]

import hmac
import base64
h = hmac.new('hello world').digest()
print( base64.b64encode( h ) )
# sGXfsWEMawIp9we7B/iuuA==
import hmac
import base64
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).hexdigest()
print( h )
# 03376ee7ad7bbfceee98660439a4d8b125122a5a
import hmac
import base64
import hashlib
h = hmac.new('secret', 'hello world', hashlib.sha1).digest()
print( base64.b64encode( h ) )
# Azdu5617v87umGYEOaTYsSUSKlo=

4 Perl[ | ]

use Digest::SHA qw(hmac_sha1_base64);
my $digest = hmac_sha1_base64("hello world", "secret");

while (length($digest) % 4) {
    $digest .= '=';
}

print $digest . "\n";
# Azdu5617v87umGYEOaTYsSUSKlo=

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}