如何在Java中生成相当于Python示例的HMAC?(How to generate an HMAC in Java equivalent to a Python example?)
我正在考虑通过Oauth在Java中实现一个获得Twitter授权的应用程序。 第一步是获取请求令牌 。 这是一个应用程序引擎的Python示例 。
为了测试我的代码,我运行Python并使用Java检查输出。 以下是生成基于哈希的消息验证码(HMAC)的Python示例:
#!/usr/bin/python from hashlib import sha1 from hmac import new as hmac key = "qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50" message = "foo" print "%s" % hmac(key, message, sha1).digest().encode('base64')[:-1]
输出:
$ ./foo.py +3h2gpjf4xcynjCGU5lbdMBwGOc=
在Java中如何复制这个例子?
我在Java中看到了一个HMAC的例子 :
try { // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104 // In practice, you would save this key. KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey key = keyGen.generateKey(); // Create a MAC object using HMAC-MD5 and initialize with key Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); String str = "This message will be digested"; // Encode the string into bytes using utf-8 and digest it byte[] utf8 = str.getBytes("UTF8"); byte[] digest = mac.doFinal(utf8); // If desired, convert the digest into a string String digestB64 = new sun.misc.BASE64Encoder().encode(digest); } catch (InvalidKeyException e) { } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { }
它使用javax.crypto.Mac , 一切都好 。 但是, SecretKey构造函数采用字节和算法。
Python示例中的算法是什么? 如何在没有算法的情况下创建Java密钥?
I'm looking at implementing an app getting Twitter authorization via Oauth in Java. The first step is getting a request token. Here is a Python example for app engine.
To test my code, I am running Python and checking output with Java. Here is an example of Python generating a Hash-Based Message Authentication Code (HMAC):
#!/usr/bin/python from hashlib import sha1 from hmac import new as hmac key = "qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50" message = "foo" print "%s" % hmac(key, message, sha1).digest().encode('base64')[:-1]
Output:
$ ./foo.py +3h2gpjf4xcynjCGU5lbdMBwGOc=
How does one replicate this example in Java?
I've seen an example of HMAC in Java:
try { // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104 // In practice, you would save this key. KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey key = keyGen.generateKey(); // Create a MAC object using HMAC-MD5 and initialize with key Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key); String str = "This message will be digested"; // Encode the string into bytes using utf-8 and digest it byte[] utf8 = str.getBytes("UTF8"); byte[] digest = mac.doFinal(utf8); // If desired, convert the digest into a string String digestB64 = new sun.misc.BASE64Encoder().encode(digest); } catch (InvalidKeyException e) { } catch (NoSuchAlgorithmException e) { } catch (UnsupportedEncodingException e) { }
It uses javax.crypto.Mac, all good. However, the SecretKey constructors take bytes and an algorithm.
What's the algorithm in the Python example? How can one create a Java secret key without an algorithm?
原文:https://stackoverflow.com/questions/3208160
最满意答案
错误消息告诉您,无法找到FXML文件。
你可以尝试:
改变这个......
public static final String MAIN_SCREEN_FXML = "gui\\MainMenu.fxml";
... 至 ...
public static final String MAIN_SCREEN_FXML = "/gui/MainMenu.fxml";
并改变这个......
FXMLLoader myLoader = new FXMLLoader(); File f = new File(file); URL url = f.toURI().toURL(); myLoader.setLocation(url);
...到(你不需要变量
file
和f
)......FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
一些参考:
- 我在这里有一个关于如何使用getResource的答案。
- 您还可以查看getResource()的文档
- 您可以查看有关从JAR加载资源的问题
What the error message tells you, that the FXML file cannot be located.
You could try to:
Change this ...
public static final String MAIN_SCREEN_FXML = "gui\\MainMenu.fxml";
... to ...
public static final String MAIN_SCREEN_FXML = "/gui/MainMenu.fxml";
And to change this ...
FXMLLoader myLoader = new FXMLLoader(); File f = new File(file); URL url = f.toURI().toURL(); myLoader.setLocation(url);
... to (and you don't need the variables
file
andf
)...FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Some references:
- I had an answer here on how to use getResource.
- Also you can check the documentation of getResource()
- And you can check this question about loading resources from a JAR
相关问答
更多-
当适配器属于ViewPager时,我遇到了同样的问题 这段代码解决了我的问题 @Override public void onLoadFinished(Loader
loader, Cursor data) { // to be restored after reload mInitialScrollPosition = mViewPager.getCurrentItem(); // do change the data ... -
Java:尚未加载屏幕(Java: Screen has not been loaded)[2023-08-01]
错误消息告诉您,无法找到FXML文件。 你可以尝试: 改变这个...... public static final String MAIN_SCREEN_FXML = "gui\\MainMenu.fxml"; ... 至 ... public static final String MAIN_SCREEN_FXML = "/gui/MainMenu.fxml"; 并改变这个...... FXMLLoader myLoader = new FXMLLoader(); File f = new Fil ... -
最简单的选择是弹出一个模态对话框: JDialog modalDialog = new JDialog(frame, "Busy", ModalityType.DOCUMENT_MODAL); modalDialog.setSize(200, 150); modalDialog.setLocationRelativeTo(frame); modalDialog.setVisible(true); 您可能需要在其中放置一个进度条。 The simplest option is to pop up a mod ...
-
定义所拥有的表单是否已在其所有者表单的同一屏幕上加载(Define if an owned form has loaded at the same screen of it's owner form)[2022-07-27]
您可以测试从Screen.FromControl(Me)返回的Screen对象 Private Sub ScreenTestButton_Click(sender As Object, e As EventArgs) Handles ScreenTestButton.Click Try Dim thisScreen As Screen = Screen.FromControl(Me) If thisScreen.Primary Then ... -
请打开以下链接以获取上述问题的答案:) https://gis.stackexchange.com/questions/74061/geoserver-map-layers-tiles-loaded-in-the-screen-or-not/74069?noredirect=1#comment101451_74069 Please open the below link for getting Answer for the Above Question :) https://gis.stackexchang ...
-
检查文件路径的大小写。 模拟器(默认情况下)不区分大小写,而设备是。 这可能导致模拟器成功找到文件,但设备失败。 Check the case of your file paths. The simulator (by default) is not case sensitive, whereas the device is. This could cause the simulator to successfully find the file, but the device to fail.
-
你可以随时检查窗口的大小,无论是高度还是宽度,浮在你的船上的任何东西,并添加一些图像文件名来加载高分辨率图像,例如将img4.jpg作为普通图像,将img4_big.jpg作为高分辨率图像等。 看起来像这样: $(window).load(function() { var randomImages = ['img1', 'img2', 'img3', 'img4', 'img5']; var rndNum = Math.floor(Math.random() * randomImages.l ...
-
import flash.display.Loader; import flash.display.Sprite; import flash.net.URLRequest; public class Image { private var _loader:Loader; public function Image(src:String) { _loader = new Loader(); _loader.load(new URLRequest(src)) ...
-
lproj文件尚未加载(lproj file not loaded yet)[2023-09-16]
制作扩展为此 extension String { func localized(lang:String) -> String { let path = Bundle.main.path(forResource: lang, ofType: "lproj") let bundle = Bundle(path: path!) return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comm ... -
听起来你需要在视图之间共享状态。 我使用一个StateStore类,它被注入到任何希望共享状态的视图中。 默认情况下,所有注入的对象都是Singletons ,可以轻松共享状态。 一个非常简单的例子可能是(在TypeScript中): statestore.ts export class StateStore { state: any; } masterview.ts autoinject() export class MasterView { constructor(private st ...